2

I am trying to auto-open any contacts menu on the page: as example I am going to "http://www.bawnlodge.co.uk/" page - then I would like to click on "Contact" tab

ATM I tried various approaches like:

driver.find_element_by_xpath("//*[contains(text(), 'onta')]").click()

or

driver.find_element_by_xpath('//a[contains(@href, "onta")]').click()

(and few similar)

however so far, I was unable to click the element

I would be grateful if anyone could explain me why I am failing here :/

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

0

Try WebDriverWait and follwoing locator strategy.

element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,'//div[@class="right"]//ul[@id="menu-header-right"]//li/a[contains(.,"Contact")]')))
element.click()

You need to have following imports to work above code.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

You could use a faster class or id css selector

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.menu-item-26 a'))).click()

or

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#menu-item-100 a'))).click()

You could also simply concatenate the string "contact" onto "http://www.bawnlodge.co.uk/" and .get to that. Multi word tab names are joined by "-" e.g. lodge-bar-and-kitchen. Everything is lowercase.

QHarr
  • 83,427
  • 12
  • 54
  • 101
0

To invoke click() on the element with text as CONTACT using part of the href attribute you need to induce WebDriverWait for the desired 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, "div.container a[href*='contact']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='container']//a[contains(@href, 'contact')]"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352