很难点击激活下拉菜单的单选按钮。使用Selenium和Python

问题描述 投票:0回答:1

所以通常我不会很烦单选按钮,但是我试图将Web浏览器设置的主页配置为Chrome设置中的自定义URL。我知道我必须激活允许我使用的下拉框(提供的图片)。我找到了我认为是来源的地方,不得不浏览一些影子DOM。但是,在到达路径后,我尝试单击它,我得到了错误

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (311, 1418)

我很困惑,我一直在努力解决这一问题。有人知道吗?当我亲自单击其他选项时,我确实注意到某些设置发生了变化。这是图片

enter image description here

enter image description here

[这里输入我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
def expand_shadow_element(element):
    shadow_root = cdriver.execute_script('return arguments[0].shadowRoot', element)
    return shadow_root
#chrom driver
cdriver = webdriver.Chrome(executable_path='C:\\Users\\name\Downloads\\chromedriver.exe')
#open up page to chrome settings.
cdriver.get('chrome://settings/')

root1 = cdriver.find_element_by_tag_name('settings-ui')
shadow_root1 = expand_shadow_element(root1)

root2 = shadow_root1.find_element_by_id('main')
shadow_root2 = expand_shadow_element(root2)

root3 = shadow_root2.find_element_by_tag_name('settings-basic-page')
shadow_root3 = expand_shadow_element(root3)

root4 = shadow_root3.find_element_by_tag_name('settings-on-startup-page')
shadow_root4 = expand_shadow_element(root4)

root5 = shadow_root4.find_element_by_name('4')
shadow_root5 = expand_shadow_element(root5)

root6 = shadow_root5.find_element_by_id('button')
root6.click()

任何人都知道为什么我不能单击源吗?我什至右击单选按钮,多数民众赞成在我指向的来源。

python selenium selenium-webdriver selenium-chromedriver shadow-dom
1个回答
0
投票

要解决元素单击被拦截的错误,您可以尝试单击Javascript并查看是否适合您。

DOM树有点难以理解,但是我想我在这里遇到了麻烦-在代码示例中突出显示了controlled-radio-button元素下的单选设置,小圆圈本身是<div class='disc'>。] >

[我没有看到存储在<div id="button">中的root6下的影子根-我看到了元素,但没有看到影子根,所以我假设单选按钮本身实际上位于root5下。] >

话虽这么说,以下是一些使用Javascript单击单选按钮(给出其描述)的代码:

# grab the radio element... tricky with shadow roots
radio_button = root5.find_element_by_xpath("//div[@id='button']/div[@class='disc']")

# attempt to click it using JS -- ignores ClickIntercepted error
driver.execute_script("arguments[0].click();", radio_button)

由于您已经找到了正确的单选组并将阴影元素存储在root5变量中,因此您可以尝试使用find_element_by_xpath上的root5来抓住出现在其下的<div class="disc">元素。 Javascript单击有望应能解决ClickIntercepted错误。

© www.soinside.com 2019 - 2024. All rights reserved.