【问题标题】:Looping through webelements with selenium Python使用 selenium Python 循环访问 webelements
【发布时间】:2019-11-21 15:43:05
【问题描述】:

我目前正在尝试使用 Selenium 和 python 来自动化一个过程,但我遇到了障碍。该列表是位于树下的列表的一部分。我已经用以下 xpath 确定了树的基础

item = driver.find_element_by_xpath("//*[@id='filter']/ul/li[1]//ul//li")

items = item.find_elements_by_tag_name("li")

我正在尝试遍历“项目”部分,但需要并单击任何带有“输入”标签的内容

for k in items: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((k.find_element(By.TAG_NAME, "input")))).click()

执行上述操作时,出现以下错误: "TypeError: * 后面的 find_element() 参数必须是可迭代的,而不是 WebElement"

由于某种原因,如果我使用类似下面的内容,.click() 将不起作用。 k.find_element_by_tag_name("input").click() 只有当我使用 webdriverwait 时它才有效。每当我需要单击页面上的某些内容时,我都必须使用网络驱动程序等待方法。

我的问题是: 复制items = item.find_elements_by_tag_name("li")的语法是什么 对于WebDriverWait(driver, 10).until(EC.element_to_be_clickable((k.find_element(By.TAG_NAME, "input")))).click() 即我如何使用基本路径并附加到使用私有方法find_elements(By.TAG_NAME)

提前致谢

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗?

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

我已经设法找到解决方法并让 Selenium 做我需要的事情。 我不得不调用 javascript 执行,而不是试图得到 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((k.find_element(By.TAG_NAME, "input")))).click() 上班,我刚用过 driver.execute_script("arguments[0].click();", k.find_element_by_tag_name("input"))

它正在做我需要它做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2022-01-25
    相关资源
    最近更新 更多