HTML
<span class="menu-text">Download App</span>
我的定位元素的代码
driver.find_element_by_css_selector("//span[contains(text(),'App')]")
或
driver.find_element_by_css_selector("//span[contains(.,'App')]")
当我运行它时,报告了一个异常:
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')]"
最佳答案
由于您使用的是 xpath 表达式,因此请改用 driver.find_element_by_xpath
driver.find_element_by_xpath("//span[contains(text(),'App')]")
希望对你有帮助
关于python - 如何使用面向异常的 text() 定位元素(selenium + python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59278150/