Home > Back-end >  Variable input from lists for find_element selenium function
Variable input from lists for find_element selenium function

Time:11-17

Hi StackOverflow gurus,

I am new to coding and Python but very enthusiastic about it. Your support and option will be huge addition do my development.

I am trying to write a Python code, where using Selenium find_element(By.LINK_TEXT,"") I need to identify company names and click on it. This action should be repetitive for all the companies on the list (in total I have around 60 entities on the list, but for this example I am using only 3). For this I used the loop. But as a result I am getting an error:

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

These actions should be performed in Google Chrome browser.

This is what I have documented so far:

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()

I am getting an error on this the last line:

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

If I manually include the value it works e.g.:

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

Could you share your suggestions how to fix this?

CodePudding user response:

You're using the entire company list as your text. Use the index you created in the for loop to grab only one element in the list:

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