1

I'm using selenium to grab all support page of some site.On one particular page i have one error . Here the code of the page :

<a href="?a=support" class="">
<span><b></b> Contact</span>
<b></b> Contact
</a>

I'm trying with many try / except condition to find a support page.

try:
    print ("FINDING SUPPORT ...")
    driver_hyip.find_element_by_css_selector("a[href*='support']").click()
    driver_hyip.execute_script("window.alert = function() {};")
except:
try:
    print ("NOT FINDING SUPPORT, TRYING ANOTHER WAY")
    driver_hyip.find_element_by_xpath("//a[@href='?a=support']").click()
except:
    print ("NOT FINDING SUPPORT, TRYING TO FIND CONTACT")
    driver_hyip.find_element_by_css_selector("a[href*='contact']").click()

I expected my code find the page, but I got :

Message: no such element: Unable to locate element: {"method":"css selector","selector":"a[href*='contact']"}

PS : Do you know if there is some other way to try multiple path in selenium ? Because here i'm doing lot of try / except in my code.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Valentin Garreau
  • 963
  • 1
  • 10
  • 32

1 Answers1

1

To click() on the element with text as Contact 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, "a[href$='support']>span"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'support')]/span"))).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