1

There is the following page.

http://remitly.com/us/en/

When you click on a select, a list with countries appears. I try to select one country, for example, Colombia and click on it. But I get an error.

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]' is not a valid XPath expression.

select = driver.find_element_by_class_name('f1wrnyr7')
select.click()
countries = driver.find_element_by_class_name('f1o6pohl')
country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]")
unknown
  • 63
  • 1
  • 5
  • 26

4 Answers4

1

perhaps you were trying something like this (change xpaths as follows, based on your needs):

notice here that the text node should be equal to 'Colombia': //span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']

or, the text node might contain some long text, but should also contain 'Colombia' in that text:

//span[contains(@class, 'md_countryName_fdxiah8') and contains(text(), 'Colombia')]

ruhaib
  • 573
  • 3
  • 9
  • But when I click on an element, I get an `element not visible` error. tried to set `time.sleep(5)` – unknown Aug 20 '19 at 09:27
1

Looks like you forgot to attach a link to the page. Anyway, the XPath expression is invalid, the corrected version may be:

//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']\

You can test it with the following XML:

<span class="md_countryName_fdxiah8">Colombia</span>

Result:

Element='<span class="md_countryName_fdxiah8">Colombia</span>'
Oleh Rybalchenko
  • 3,977
  • 2
  • 13
  • 30
  • But when I click on an element, I get an `element not visible` error. tried to set `time.sleep(5)` – unknown Aug 20 '19 at 09:27
  • Would be great to see the target page, can't see the link in your question. Maybe you need something like `wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))` – Oleh Rybalchenko Aug 20 '19 at 09:34
1

This error message...

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//span[contains(@class, 'md_countryName_fdxiah8' and text(), 'Colombia')]' is not a valid XPath expression.

...implies that the XPath which you have used was not a valid XPath expression.

Seems you were pretty close. You can use either of the following Locator Strategies:

  • Using xpath 1:

    country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")
    
  • Using xpath 2:

    country = countries.find_element_by_xpath("//span[contains(@class, 'md_countryName_fdxiah8') and contains(., 'Colombia')]")
    

Here you can find a relevant discussion on SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression


Update

To overcome the element not visible error you need to induce WebDriverWait for visibility_of_element_located() and you can use either of the following Locator Strategy:

element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • But when I click on an element, I get an `element not visible` error. tried to set `time.sleep(5)` – unknown Aug 20 '19 at 09:27
  • @ДмитрийДмитрук Checkout the answer update and let me know the status. – DebanjanB Aug 20 '19 at 09:32
  • `raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:` – unknown Aug 20 '19 at 09:36
  • @ДмитрийДмитрук Checkout the answer update and let me know the status. – DebanjanB Aug 20 '19 at 09:42
  • Check the change log. After observing the url I have shifted the loyalty from `countries` to `driver`. – DebanjanB Aug 20 '19 at 09:45
  • I wrote `element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']")))` and `element.click()` and got error `raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:` – unknown Aug 20 '19 at 09:53
  • @ДмитрийДмитрук There wasn't any clicking attempt in your code. Why would you attempt a click? Definitely answer would be different. – DebanjanB Aug 20 '19 at 09:57
  • Sorry. But I do not understand why an error flies on this line ... `element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@class, 'md_countryName_fdxiah8') and text()='Colombia']"))).sendKeys(Keys.Enter)` – unknown Aug 20 '19 at 10:06
  • @ДмитрийДмитрук Now you are messing up things. `sendKeys()` requires element to be clickable. This should be a new question altogether. – DebanjanB Aug 20 '19 at 10:16
0

From my experience, Select element are better handled with Select function object. Then each of the list contnet can be addressed by its text

You should import:

from selenium.webdriver.support.ui import Select

And then

select = Select(driver.find_element_by_class_name('f1wrnyr7'))
select.select_by_visible_text('Colombia')
Moshe S.
  • 2,134
  • 2
  • 11
  • 25