0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Ty for your support, unfortunately clicking the element and switching to the next page still doesn't function.
@Numpy Fixed it now. print(company) is now printing Nutanix from the redirected page
Thank you, but I am getting following error: File "D:\... <module> elements = WebDriverWait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[data-cy-id="search-results-list"] p'))) TypeError: until() missing 1 required positional argument: 'method' Unfortunately element.click does not redirect to the next page.
That's because you didn't used wait variable but WebDriverWait. WebDriverWait is a class and needs to be instantiated, as a I did above with wait = WebDriverWait(driver, 10)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.