繁体   English   中英

来自 find_element 列表的变量输入 selenium function

[英]Variable input from lists for find_element selenium function

嗨 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 = ['Apple Inc','Microsoft','Tesla']


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,'Tesla').click()

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM