很难点击单选按钮来激活下拉列表。使用Selenium和Python

2024-06-01 03:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

所以通常我对单选按钮没有困难,但是我试着从chrome设置中配置web浏览器设置主页来定制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()

有人知道我为什么不能点击来源吗?我甚至右键点击了单选按钮,这就是我被指向的来源


Tags: namebysettings来源elementfindchromeexpand
1条回答
网友
1楼 · 发布于 2024-06-01 03:19:05

要解决元素click截获的错误,您可以尝试Javascript click,看看这是否适合您

DOM树有点难理解,但是我想我在这里理解了一个漂移,radio设置在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变量中,所以可以尝试使用root5上的find_element_by_xpath来获取出现在它下面的<div class="disc">元素。Javascript的click应该能够解决ClickIntercepted错误

相关问题 更多 >