Reputation: 366
I know there are similar questions but I couldn't manage to utilize the solutions. That's why I am asking for my specific problem.
My code looks like:
driver.get("https://")
driver.maximize_window()
login = driver.find_element_by_xpath(email_xpath).send_keys(email)
login = driver.find_element_by_xpath(pwd_xpath).send_keys(pwd)
login = driver.find_element_by_xpath(continue_xpath)
login.click()
time.sleep(10)
ticker = 'DB:0NU'
search = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, search_xpath))).send_keys(ticker)
time.sleep(10)
print(search)
elements = driver.find_elements_by_xpath("//*[@id='popup-portal']/div/div/div/div/ul")
for element in elements:
print(element.text)
try:
if element.text == ticker:
element.click()
except:
pass
I don't get any error results, but nethertheless, the solution is not satisfying, because my code doesn't select 'DB:0NU' from the search result, even though one can see it in the search result. How can I make my code click the element?
A sample of the html source code:
<ul data-cy-id="search-results-list" class="sc-gCwZxT kelPjw"><li>
Anyone who can help?
Upvotes: 0
Views: 810
Reputation: 2394
You had some issues with the locators on the page, the ones that get the search results.
Besides this, in the for
loop you need a break
when the desired value is found because you don't want to continue searching for other results once you found it.
driver.get("https://simplywall.st/dashboard")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[name="username"]'))).send_keys(email)
driver.find_element_by_css_selector('[name="password"]').send_keys(password)
driver.find_element_by_css_selector('[data-cy-id="button-submit-login"]').click()
ticker = 'DB:0NU'
search = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[name="search-search-field"]'))).send_keys(ticker)
elements = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[data-cy-id="search-results-list"] p')))
for element in elements:
print(element.text)
try:
if element.text == ticker:
element.click()
company = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '[data-cy-id="company-header-title"]'))).text
print(company)
break
except:
pass
Upvotes: 1