0
votes

I'm trying to locate the below element :

<span class="btn btn-default" onclick="close_terms_window();" style="" xpath="1">Next</span>

using selenium webdriver. But I was getting this message:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id='terms-modal']/div//[@id='acceptterms']//[@onclick="close_terms_window();"]"} (Session info: chrome=74.0.3729.131) (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64)

I used the below methods:

Method 1:

time.sleep(4) 
element_term = driver.find_element_by_xpath("//div[@id='terms-modal']/div//* [@id='acceptterms']//span[@onclick=\"close_terms_window();\"]")

Method 2:

element_term = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,"//span[contains(@class,'btn') and contains(@class,'btn-default)]")))`

Method 3:

time.sleep(1)
element_term = driver.find_element_by_xpath("//div[@id='terms-modal']/div//*[@id='acceptterms']//*[@onclick=\"close_terms_window();\"]")

Method 4:

Using CSS selector:

time.sleep(4)
driver.find_element_by_css_selector("span.btn btn-default")
element_term = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,"//span[contains(@class,'btn') and contains(@class,'btn-default)]")))

Source Code snapshot: https://i.stack.imgur.com/tib3V.png

2
Apologies for the messy format . I am new to stackoverflow. Please ignore the formatting . - charanya chinnasamy
Welcome to SO. Have you tried with //span[.='Next' and @class=' btn btn-default']? - supputuri
Thank You . Yeah i tried but now luck . Getting the below error: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[.='Next' and @class=' btn btn-default']"} - charanya chinnasamy
//span[@class='btn btn-default'] try with this it will work. - Dhru 'soni
That's great ! //span[@class='btn btn-default'] works fine . Earlier I tried the same without sleep , now i tried with sleep so this works fine now . Is there any other way to avoid sleep as it's not good practice . In case of explicit wait which expected condition would work? - charanya chinnasamy

2 Answers

0
votes
from selenium.webdriver.support import expected_conditions as EC


WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='btn btn-default')))

Expected Conditions :

These are some common conditions which are frequently used when automating web applications.

Selenium Python binding provides some convenience methods so you don’t have to code an expected_condition class yourself or create your own utility package for them.

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of_element_located
  • visibility_of
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • invisibility_of_element_located
  • element_to_be_clickable
0
votes

As the element is within a Modal Dialog Box to locate and click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.btn.btn-default[onclick^='close_terms_window']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='btn btn-default' and starts-with(@onclick,'close_terms_window')][contains(., 'Next')]")))
    
  • 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