【问题标题】:Variable input from lists for find_element selenium function来自 find_element selenium 函数列表的变量输入
【发布时间】:2022-11-16 22:36:52
【问题描述】:

嗨 StackOverflow 专家们,

我是编码和 Python 的新手,但对它非常热衷。您的支持和选择将是我发展的巨大补充。

我正在尝试编写 Python 代码,其中使用 Selenium find_element(By.LINK_TEXT,"") 我需要识别公司名称并单击它。对列表中的所有公司都应该重复此操作(我在列表中总共有大约 60 个实体,但对于这个例子我只使用了 3 个)。为此,我使用了循环。 但结果我收到一个错误:

driver.find_element(By.LINK_TEXT,format(str(company))).click()    #Select the entity. This input must be later variable. Items are foudn with link text

TypeError: 'str' object is not callable

这些操作应在 Google Chrome 浏览器中执行。

这是我到目前为止记录的内容:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select


company = ['Company1','Company2','Company3']


url = "I did not include the link due to security reasons"


driver = webdriver.Chrome(r"C:\Users\Downloads\chromedriver_win32\chromedriver.exe")
driver.get(url)

drop = Select(driver.find_element(By.ID,'ctl00_Cont_uxProjectTTIDropDownList')) #select project from droop down list
drop.select_by_visible_text ('2022 Q4 - Projects') 

sleep(1)

for i in range (len(company)):
    driver.find_element(By.LINK_TEXT,format(str(company))).click()

我在最后一行收到错误消息:

for i in range (len(company)):
    driver.find_element(By.LINK_TEXT,format(str(company))).click()

如果我手动包含它起作用的值,例如:

driver.find_element(By.LINK_TEXT,'Compan1').click()

你能分享你的建议如何解决这个问题吗?

【问题讨论】:

    标签: python list selenium dynamic findelement


    【解决方案1】:

    您正在使用整个公司列表作为文本。使用您在 for 循环中创建的索引仅获取列表中的一个元素:

    for i in range (len(company)):
        driver.find_element(By.LINK_TEXT,company[i]).click()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2022-12-07
      • 2014-11-09
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多