python - 如何使用变量 driver.find_element_by_id 的值

标签 python selenium selenium-webdriver automation

我有以下代码:

for i in range(69,96,1):
    loztreeid = "loztree_" + str(i) + "_check"
    driver.find_element_by_id(loztreeid).click()
    time.sleep(0.5)

我需要点击名为loztree_69_check的ID到loztree_95_check。当我尝试使用上面的代码时,它给出了以下错误:

File "gpsdemo.py", line 41 driver.find_element_by_id(loztreeid).click()

最佳答案

我找到了一种方法,尝试使用下面的代码并且工作正常:

for i in range(69,96,1):
driver.find_element_by_id('loztree_' + str(i) + '_check').click()

我不知道为什么双引号(")不起作用,只需更改为单引号(')即可。

关于python - 如何使用变量 driver.find_element_by_id 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51530240/

相关文章:

Python 在某个字符前拆分

python - 从位于网站的图表中解析表格项时遇到问题

java - 如何使用 Selenium Webdriver 访问其 xpath 发生变化的 WebElement

python - 使用selenium python查找元素锚定类元素导致css选择器错误

javascript - 使用 selenium webdriver 在 IE11 中右键/上下文单击无法正常工作

java - Java-selenium 上的 StaleElementReferenceException 错误

Selenium 服务器无法远程运行 - 启动 Firefox 时出现问题

python - 如何从数据框中提取父节点和后续节点

python - 核心 4D 图像 tif 存储为 hdf5 python

python - numpy 设置值与另一个多维数组作为索引