我查了资料说lxml不支持xpath2.0所以不能用ends-with,所以selenium不能用ends-with怎么用或者替换ends-with。真的非常感谢!!!
HTML 示例
<span id="xxxxx_close">wwwww</span>
的'xxxxx'
部分@id
是随机的
我查了资料说lxml不支持xpath2.0所以不能用ends-with,所以selenium不能用ends-with怎么用或者替换ends-with。真的非常感谢!!!
HTML 示例
<span id="xxxxx_close">wwwww</span>
的'xxxxx'
部分@id
是随机的
XPath 约束函数是XPath v2.0的一部分,但根据当前的实现Selenium支持XPath v1.0。ends-with
根据您共享的HTML,您可以使用以下任一Locator Strategies来识别元素:
XPath
使用contains()
:
xpath
使用包含作为id
属性:
driver.findElement(By.xpath("//span[contains(@id,'_close')]")).click();
xpath
使用包含和id
属性innerHTML
:
driver.findElement(By.xpath("//span[contains(@id,'_close') and contains(.,'wwwww')]")).click();
或者,您也可以使用CssSelector,如下所示:
css_selector
对属性使用以(即$
通配符)结尾的子句:id
driver.find_element_by_css_selector("span[id$='_close']").click();
css_selector
对属性使用包含(即*
通配符)子句:id
driver.find_element_by_css_selector("span[id*='_close']").click();
您可以应用以 CSS 选择器结尾的:
By.cssSelector("[id$=_close]")
也不需要在 css 选择器搜索中包含 span 标签。