如何使用 Selenium 通过 xpath 识别动态输入复选框元素
How to identify a dynamic input checkbox element through xpath using Selenium
你好,我需要一些帮助,我正在使用 python 最新版本的 selenium。
我无法到达元素输入复选框。
这是输入:
<div class="info info13">
<input type="checkbox" name="yachi13" id="yachi13" class="input-checkbox" value="13">
</div>
我试过了:
# try 1
swClick(driver, 'xpath', "[//*@id='yachi" + teethValue + "']")
# try 2
driver.find_element_by_id("yachi" + teethValue)
# try 3
swClick(driver, 'xpath', "[//input[@name='yachi" + teethValue + "']")
# try 4
swClick(driver, 'xpath', "//div[@class='info info" + teethValue + " canClick']/input[@id='yachi" + teethValue + "']")
# try 5
swClick(driver, 'xpath', "//div[@class='info info" + teethValue + "']")
# try 6
for i in range(10):
try:
driver.find_element_by_xpath(
"//input[@id='yachi" + teethValue + "']"
).click()
break
except NoSuchElementException as e:
print('Retry in 1 second')
time.sleep(1)
else:
raise e
teethvalue 是一个数字,swClick 函数完美运行。
你能告诉我如何用经典的方式处理复选框吗?
谢谢!
您可以按如下方式使用Literal String Interpolation also known as f-string Formatting:
teethValue = "13"
print(f"//input[@name='yachi{teethValue}']")
输出:
//input[@name='yachi13']
实际上,您的代码块将是:
# teethValue = "13"
driver.find_element(By.XPATH, f"//input[@name='yachi{teethValue}']").click()
你好,我需要一些帮助,我正在使用 python 最新版本的 selenium。 我无法到达元素输入复选框。 这是输入:
<div class="info info13">
<input type="checkbox" name="yachi13" id="yachi13" class="input-checkbox" value="13">
</div>
我试过了:
# try 1
swClick(driver, 'xpath', "[//*@id='yachi" + teethValue + "']")
# try 2
driver.find_element_by_id("yachi" + teethValue)
# try 3
swClick(driver, 'xpath', "[//input[@name='yachi" + teethValue + "']")
# try 4
swClick(driver, 'xpath', "//div[@class='info info" + teethValue + " canClick']/input[@id='yachi" + teethValue + "']")
# try 5
swClick(driver, 'xpath', "//div[@class='info info" + teethValue + "']")
# try 6
for i in range(10):
try:
driver.find_element_by_xpath(
"//input[@id='yachi" + teethValue + "']"
).click()
break
except NoSuchElementException as e:
print('Retry in 1 second')
time.sleep(1)
else:
raise e
teethvalue 是一个数字,swClick 函数完美运行。 你能告诉我如何用经典的方式处理复选框吗? 谢谢!
您可以按如下方式使用Literal String Interpolation also known as f-string Formatting:
teethValue = "13"
print(f"//input[@name='yachi{teethValue}']")
输出:
//input[@name='yachi13']
实际上,您的代码块将是:
# teethValue = "13"
driver.find_element(By.XPATH, f"//input[@name='yachi{teethValue}']").click()