英文:
Unable to find and click checkbox using Selenium and Python
问题
我研究了一些类似问题的答案,但似乎无法弄清楚我特定情况的问题所在。我已经浏览了我的大部分脚本以达到我想要的目的,但授权复选框似乎无法找到。
而在Python中相当简单:
### 授权
driver.find_element('name', "d_1559640796736").click()
错误信息:
NoSuchElementException: 找不到元素:无法定位元素: {"method":"css selector","selector":"[name=\"d_1559640796736\"]"}
(Session info: chrome=109.0.5414.120)
我尝试了找到元素的每一种方式(id、css、xpath),但有可能是我的xpath写错了。我有一种感觉,这可能与它是一个 fieldset 有关。我承认我的Python要比我的html好得多。
HTML快照:
英文:
I've looked at a few different answers to similar questions, but can't seem to figure out what's wrong with my specific situation. I've gone through most of my script to get where I want, but the authorize checkbox just can't seem to be found.
And in Python pretty simply:
### Authorize
driver.find_element('name', "d_1559640796736").click()
With the error:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[name="d_1559640796736"]"}
(Session info: chrome=109.0.5414.120)
I've tried every other way to find the element (id, css, xpath), but it's possible my xpath is just wrong. I have a feeling it has to do with the fact that it is a fieldset. I admit my Python is much better than my html.
Snapshot of the HTML:
答案1
得分: 1
The classname attribute values like d_1559640796736
, h_1559640796736o1
are dynamically generated and is bound to chage sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.
解决方案
所需的元素是一个动态元素,要进行点击操作,需要使用WebDriverWait来等待element_to_be_clickable(),您可以使用以下任何一种定位策略:
-
使用_CSS选择器_:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Acknowledge'][name^='d_'][id^='d_'][id$='o1']"))).click()
-
使用_XPATH_:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Acknowledge' and starts-with(@name, 'd_')][starts-with(@id, 'h_') and contains(@id, 'o1')][starts-with(@id, 'h_') and contains(@id, 'o1')]"))).click()
-
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
英文:
The classname attribute values like d_1559640796736
, h_1559640796736o1
are dynamically generated and is bound to chage sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.
Solution
The desired element is a dynamic element, so to click you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
-
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Acknowledge'][name^='d_'][id^='d_'][id$='o1']"))).click()
-
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Acknowledge' and starts-with(@name, 'd_')][starts-with(@id, 'h_') and contains(@id, 'o1')][starts-with(@id, 'h_') and contains(@id, 'o1')]"))).click()
-
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
答案2
得分: 0
尝试使用以下XPath,而不是按名称查找:
//input@type='checkbox'
如果有多个复选框,您可以通过索引选择其中一个。
英文:
Try below xpath instead of finding it by name
//input[@type='checkbox'][1]
You can select one by it's index if there are multiple checkboxes present.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论