0

I am trying to test a web page using selenium python. All is working well but issue is encountering when clicking on navbar item

I have used:

driver.find_element_by_xpath('./li/a[. = "Log in"]')

Also have used:

driver.find_element_by_link_text('Log in')

Nothing got luck !!

The code snippet:

<div class='container'>
<div class='navigationbar__header'>
<a class='navigationbar__header__logo tracking-link' data-link-name='logo' href='/' target='_self'>
<div id='hired-brand'>HIRED</div>
</a>
</div>
<div class='navigationbar__toggle'>
<div class='navigationbar__toggle__element'>
<img alt='Menu' class='icon icon--sandwich' src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'>
</div>
<input class='navigationbar__toggle__helper' type='checkbox'>
<ul class='navigationbar__navigation'>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="employers_page" target="_self" href="/employers">For Employers</a></li>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="success_stories" target="_self" href="/success-stories">Success Stories</a></li>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="employers_resources" target="_self" href="/employers/resources">Resources</a></li>
<li class="navigationbar__item "><a class="text-medium sm-ml0 tracking-link" data-link-name="login" target="_self" href="/login">Log in</a></li>
<div class='xs-ptb1 xs-prl1 md-ptb0 md-inline-block'><li class="navigationbar__item "><a class="button button--primary tracking-link" data-link-name="signup" target="_self" href="/signup">Sign Up</a></li></div>

</ul>
</div>
</div>
</nav>

This code is visible on page inspect easily. Anyone know the better way to interact with it?

3
  • What error do you get? Commented Aug 17, 2019 at 12:10
  • Please could you provide more info. I've tried with the HTML snippet that you provided and worked fine. Maybe the element is not clickable because it's not visible. Would be great if you could provide an error that you get or link to the page. Commented Aug 17, 2019 at 14:59
  • @puchal Here you go : hired.com/… Commented Aug 17, 2019 at 17:22

2 Answers 2

1

To click() on the link with text as Log in within the website you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.navigationbar__item a[data-link-name='login'][href='/login']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='navigationbar__item ']/a[@data-link-name='login' and @href='/login']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

hired


Update

As an alternative you can use execute_script() as follows:

  • Using CSS_SELECTOR:

    driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.navigationbar__item a[data-link-name='login'][href='/login']"))))
    
  • Using XPATH:

    driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='navigationbar__item ']/a[@data-link-name='login' and @href='/login']"))))
    
Sign up to request clarification or add additional context in comments.

2 Comments

This seemed to work for you but I am getting timeout exception
@MahaWaqar Checkout the answer update and let me know the status.
0

Ok so I checked the site.

The problem is that when the window size is small there is toggle for navigation that you need to click first.

try this

from selenium.common.exceptions import NoSuchElementException

try:
    login_button = driver.find_element_by_link_text('Log in')
    login_button.click()
except NoSuchElementException:
    nav_bar_toggle = driver.find_element_by_class_name(
        'navigationbar__toggle__helper'
    )
    nav_bar_toggle.click()
    login_button = driver.find_element_by_link_text('Log in')
    login_button.click()

2 Comments

hmmm would be helpful if you said what exception you get either in your original code or here so maybe I could change the code accordingly
There coming 2 exceptions: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Log in"} and selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".navigationbar__toggle__helper"}

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.