很难单击激活下拉菜单的单选按钮。使用硒和 Python
Having a hard time clicking on a radio button that activates a drop down. Using Selenium and Python
所以通常我对单选按钮没有困难,但我试图将 Web 浏览器设置为从 chrome 设置自定义主页 URL。我知道我必须激活允许我使用的下拉框(提供的图片)。我找到了我认为是源头的地方,不得不在一些阴影 DOM 中导航。但是在到达路径后我试图点击它我得到错误
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (311, 1418)
我很困惑,我一直在努力解决这个问题。有人知道吗?当我亲自点击不同的选项时,我确实注意到一些设置发生了变化。这是图片
这是我的代码:
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()
有人知道为什么我不能点击来源吗?我什至右键单击了单选按钮,这就是我被指向的来源。
要解决元素点击拦截错误,您可以尝试 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
变量中,您可以尝试在 root5
上使用 find_element_by_xpath
来获取 <div class="disc">
元素出现在它的正下方。 Javascript 点击应该可以解决 ClickIntercepted 错误。
所以通常我对单选按钮没有困难,但我试图将 Web 浏览器设置为从 chrome 设置自定义主页 URL。我知道我必须激活允许我使用的下拉框(提供的图片)。我找到了我认为是源头的地方,不得不在一些阴影 DOM 中导航。但是在到达路径后我试图点击它我得到错误
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (311, 1418)
我很困惑,我一直在努力解决这个问题。有人知道吗?当我亲自点击不同的选项时,我确实注意到一些设置发生了变化。这是图片
这是我的代码:
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()
有人知道为什么我不能点击来源吗?我什至右键单击了单选按钮,这就是我被指向的来源。
要解决元素点击拦截错误,您可以尝试 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
变量中,您可以尝试在 root5
上使用 find_element_by_xpath
来获取 <div class="disc">
元素出现在它的正下方。 Javascript 点击应该可以解决 ClickIntercepted 错误。