DaReal
DaReal

Reputation: 637

Selenium unique element not returned by span text

On a web page, this element is the only one containing the exact text 'Volgende stap':

<span class="b-button--label ng-binding ng-scope" ng-if="arrowDirection === 'right'">Volgende stap</span>

But when I try to select (a list of) the elements containing these words using xpath:

driver.find_elements_by_xpath("//span[contains(text(),'Volgende')]")
driver.find_elements_by_xpath("//span[.='Volgende stap']")
driver.find_elements_by_xpath("//span[text()='Volgende stap']")

I keep getting an empty list (or element not found in case of find_element instead of find_elements).

This is the script leading to the html page I'm working on:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait

driver = webdriver.Chrome()
driver.get("https://www.greetz.nl/kaarten/verjaardag/man")
wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='/kaart/detail/greetz-verjaardagskaart---abracadabra/1142778148']"))).click()
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Kaart bewerken']"))).click()

Upvotes: 1

Views: 277

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193308

As per the HTML the desired element with text as Volgende stap is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.personalize--frame[src='//www.greetz.nl/editor/desktop.html']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.b-button--label.ng-binding.ng-scope[ng-if*='right'][]"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='personalize--frame' and @src='//www.greetz.nl/editor/desktop.html']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='b-button--label ng-binding ng-scope' and contains(text(),'Volgende')]"))).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

Here you can find a relevant discussion on Ways to deal with #document under iframe

Upvotes: 1

Lucas Tierney
Lucas Tierney

Reputation: 2573

It's in an iframe. Switch to it first, then try and find via text:

frame = driver.find_element_by_css_selector('frame.personalize--frame')
driver.switch_to.frame(frame)

Upvotes: 2

Related Questions