【问题标题】:loop the id in selenium python在 selenium python 中循环 id
【发布时间】:2017-12-29 04:58:11
【问题描述】:

我有脚本可以选择下面的按钮

driver.find_element_by_id("Product").click()
driver.find_element_by_id("id1").click()
driver.find_element_by_id("id1").click()
driver.find_element_by_id("id2").click()
driver.find_element_by_id("id2").click()
driver.find_element_by_id("id3").click()
driver.find_element_by_id("id3").click()
driver.find_element_by_id("id4").click()
driver.find_element_by_id("id4").click()
driver.find_element_by_id("id5").click()
driver.find_element_by_id("id5").click()
driver.find_element_by_id("id6").click()
driver.find_element_by_id("id6").click()
driver.find_element_by_id("id7").click()
driver.find_element_by_id("id7").click()

所以我在这里选择按钮产品,然后它有多个项目。 每个代码都会选择产品并取消选择它

如何循环这个 id 选择而不是编写长脚本 就像一旦选择了产品按钮,然后循环选择产品中的所有项目

【问题讨论】:

  • 使用for 循环? for i in range(1, 9): driver.find_element_by_id("id{}".format(i)).click()

标签: python loops selenium getelementbyid


【解决方案1】:

这可以减少您的代码,您可以使用所需的适当范围。

driver.find_element_by_id("Product").click()
for element_id in range(1, 8):
    id = "id{}".format(element_id)
    driver.find_element_by_id(id).click()
    driver.find_element_by_id(id).click()

【讨论】:

  • OP 要求每次点击执行两次。您需要重复最后一行。
  • 我收到此错误名称错误“未找到驱动程序”
  • 脚本运行没有任何错误。但它没有选择按钮
【解决方案2】:

@COLDSPEED 的评论为我们指明了正确的方向,@VaibhavMule 的回答近乎完美,但没有人从 Selenium 的角度考虑click() 的影响。 p>

点击()

click() 将通过 selenium.webdriver.remote.webelement 接口与文档交互。

所以 click() 方法调用会进行新鲜度检查,以确保元素引用仍然有效。这实质上决定了元素是否仍然附加到 DOM。如果此测试失败,则会抛出 StaleElementReferenceException,并且以后对该实例的所有调用都将失败。因此,在调用每个 click() 之前,我们必须诱导 WebDriverWait 并将 expected_conditions 子句设置为 element_to_be_clickable

所以到loop this id selectionsselect and deselect them,可以使用下面的代码块:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.find_element_by_id("Product").click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, "id1")))
for id in range(1, 7):
    my_id = "my_id{}".format(id)
    WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID, "my_id"))).click()
    WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID, "my_id"))).click()

【讨论】:

  • same error NameError: name 'WebDriverWait' is not defined from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support。 ui import Select from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest,重新导入时我遗漏了一些东西
  • 脚本运行没有任何错误。但它没有选择按钮
  • 那是因为您无法正确识别元​​素。分享相关的 HTML
  • 这是输出跟踪 WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID, "my_id"))).click() File "C:\Python27\lib \site-packages\selenium\webdriver\support\wait.py",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) TimeoutException: Message:
猜你喜欢
  • 2022-11-13
  • 2020-01-09
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 2020-12-20
  • 2019-07-30
相关资源
最近更新 更多