Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to locate element using text() facing exception(selenium+python)?

HTML

<span class="menu-text">Download App</span>

my code to locate the element

driver.find_element_by_css_selector("//span[contains(text(),'App')]")

or

driver.find_element_by_css_selector("//span[contains(.,'App')]")

when I run it, an exception reported:

selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "//span[contains(text(),'App')]" is invalid: InvalidSelectorError: '//span[contains(text(),'App')]' is not a valid selector: "//span[contains(text(),'App')]"
like image 447
elf Avatar asked Sep 04 '26 03:09

elf


1 Answers

Since your using a xpath expression, use driver.find_element_by_xpath instead

driver.find_element_by_xpath("//span[contains(text(),'App')]")

hope this helps

like image 151
Melan Avatar answered Sep 05 '26 18:09

Melan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!