I have a dataframe with ticker names and IDs, from here I have to first select the sector name and then the symbol both of which I'm nest looping through, then I'm brute forcing through the dataframe tickers to match with the resulting table that pops up.
for sec in sector.options[1:]:
sec.click()
for sym in symbol.options:
table = sym.text
for x in range(len(df)):
if df['Ticker'][x] == table:
#SEARCH ELEMENT BY TEXT AND THEN CLICK ON IT
#text= find_element_by_css_selector('span:contains(df['Ticker'][x])')
#text = find_element_by_xpath('//span[contains(text(), df['Ticker'][x]]').click()
My problem is after the if statement, I want to be able to search for the element text and then click on each of the matching tickers one by one and then copy the data from the table that pops up. I'm trying to use find_element_by_xpath but I dont know how to make the contains(text()) bit python formattable here? Any help would be appreciated
Code for running on your end:
chromedriver = "chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.psx.com.pk/psx/resources-and-tools/listings/listed-companies")
driver.maximize_window()
sector = Select(driver.find_element_by_id("sector"))
symbol = Select(driver.find_element_by_id("Symbol"))
for sec in sector.options[1:]:
sec.click()
for sym in symbol.options:
table = sym.text
for x in range(len(df)):
if df['Ticker'][x] == table:
#element1 = find_element_by_css_selector('span:contains("View All Companies")') #How to make this click on the text in the table
#element1 = find_element_by_xpath('//span[contains(text(), df]')
driver.close
CodePudding user response:
You can target addressbookdata
id since it's unique in HTML and then call the get_attribute
to capture the innerText
Code:
wait = WebDriverWait(driver, 30)
print(wait.until(EC.presence_of_element_located((By.ID, "addressbookdata"))).get_attribute('innerText'))
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC