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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Maha Waqar
  • 585
  • 1
  • 10
  • 24
  • What error do you get? – Sers Aug 17 '19 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. – puchal Aug 17 '19 at 14:59
  • @puchal Here you go : https://hired.com/join?utm_campaign=(b2c)(l-uk-ldn)(r-swe)(t1v1)&utm_content=mobiledeveloper&rx_source=reach-adview&rx_campaign=reach72&rx_job=mobiledeveloper-barnet-england-20190804&rx_cid=3105&utm_source=recruitics_reach&utm_medium=cpc – Maha Waqar Aug 17 '19 at 17:22

2 Answers2

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']"))))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
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()
puchal
  • 1,883
  • 13
  • 25
  • 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 – puchal Aug 18 '19 at 10:09
  • 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"}```` – Maha Waqar Aug 18 '19 at 10:45