Python 单击标签Selenium automation
我在使用selenium选择此软件中的RUC选项时遇到问题,标签不包含区分它们的ID,并且类是相同的: 我正在使用以下代码,但它不适用于我:Python 单击标签Selenium automation,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我在使用selenium选择此软件中的RUC选项时遇到问题,标签不包含区分它们的ID,并且类是相同的: 我正在使用以下代码,但它不适用于我: label_tipo_documento = driver.find_element_by_xpath('//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"][2]
label_tipo_documento = driver.find_element_by_xpath('//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"][2]')
print(label_tipo_documento.text)
label_tipo_documento.click()
label_tipo_documento = driver.find_element_by_xpath('(//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"])[2]')
print(label_tipo_documento.text)
label_tipo_documento.click()
我尝试在标签[2](RUC)中选择第二个选项,并收到以下消息:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"][2]"}
(Session info: chrome=87.0.4280.88)
但是,当我使用[]而不是[2]执行此操作时,它会找到一个值:
DNI
使用以下
xpath
单击第二个单选按钮
label_tipo_documento = driver.find_element_by_xpath("//label[./input[@value='RUC']]")
print(label_tipo_documento.text)
label_tipo_documento.click()
如果你想使用索引,那么试试下面的代码 表示要选择
div
节点的第二个子体标签(它只有一个子体标签)。而您需要选择标签的第二次出现。所以要么试试
label_tipo_documento = driver.find_element_by_xpath('(//div[@id="form-group-tipo_documento"]//div[@class="col-sm-3"]//label[@class="radio-inline"])[2]')
或
早上好
因此,基于您的图像,我相信您需要的xpath
就是这个
# XPATH
//div[contains(@id, 'tipo_documento')]//label[@class='radio-inline']//input[@value='DNI']
基于此xpath
,您应该为要单击的单选按钮创建一个变量,然后使用以下命令执行它
myValue = "DNI"
driver.find_element(By.XPATH, "//div[contains(@id, 'tipo_documento')]//label[@class='radio-inline']//input[@value='{0}']".format(myValue)).click()
根据此代码,您可以将“DNI”更改为“RUC”或“CEX”,这样您就可以开始了。要单击与文本关联的元素作为RUC,您可以使用以下任一选项:
- 对
使用css\u选择器:
- 使用
xpath
和
文本:
理想情况下,要单击需要引导的元素,使元素可单击()
,您可以使用以下任一选项:
- 使用
CSS\u选择器
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#form-group-tipo_documento label.radio-inline > input[value='RUC']"))).click()
- 使用
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='form-group-tipo_documento']//label[@class='radio-inline' and contains(., 'RUC')]/input"))).click()
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
工具书类
您可以在以下内容中找到有关的讨论:
这会起作用,最好使用xpath查找元素,然后用python而不是xpath进行索引。@DJG如果这个/任何答案对您有帮助,请告诉我,以便将来的读者。
driver.find_element_by_xpath("//div[@id='form-group-tipo_documento']//label[@class='radio-inline' and contains(., 'RUC')]/input").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#form-group-tipo_documento label.radio-inline > input[value='RUC']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='form-group-tipo_documento']//label[@class='radio-inline' and contains(., 'RUC')]/input"))).click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC