1

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
DaReal
  • 597
  • 3
  • 10
  • 24
  • so if I go to that page and manually click on `/kaart/detail/greetz-verjaardagskaart---abracadabra/1142778148` and search for `Volgende` on either visible page, or page source, nothing comes up. – timbre timbre Nov 13 '18 at 23:36

2 Answers2

2

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)
Lucas Tierney
  • 2,503
  • 15
  • 13
1

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I see, so it was because of an Iframe. Thanks for the explanation and link to more information. – DaReal Nov 14 '18 at 18:56