Python 如何在selenium中使用函数结尾?

Python 如何在selenium中使用函数结尾?,python,selenium,xpath,css-selectors,ends-with,Python,Selenium,Xpath,Css Selectors,Ends With,我查找了lxml不支持xpath2.0的信息,因此它不能将ends与一起使用,因此selenium不能将ends与如何使用它或将ends替换为一起使用。真的非常感谢你 HTML示例 <span id="xxxxx_close">wwwww</span> wwwww @id的'xxxxx'部分是随机的是XPath v2.0的一部分,但根据当前的实现,Selenium支持XPath v1.0 根据您共享的HTML,确定您可以使用以下任一元素: XPath使用: xpa

我查找了lxml不支持xpath2.0的信息,因此它不能将ends与一起使用,因此selenium不能将ends与如何使用它或将ends替换为一起使用。真的非常感谢你

HTML示例

<span id="xxxxx_close">wwwww</span>
wwwww
@id
'xxxxx'
部分是随机的

XPath v2.0的一部分,但根据当前的实现,Selenium支持XPath v1.0

根据您共享的HTML,确定您可以使用以下任一元素:

  • XPath
    使用:

    • xpath
      使用contains for
      id
      属性:

      driver.findElement(By.xpath("//span[contains(@id,'_close')]")).click();
      
      driver.findElement(By.xpath("//span[contains(@id,'_close') and contains(.,'wwwww')]")).click();
      
      driver.find_element_by_css_selector("span[id$='_close']").click();
      
      driver.find_element_by_css_selector("span[id*='_close']").click();
      
    • xpath
      使用contains for
      id
      innerHTML
      属性:

      driver.findElement(By.xpath("//span[contains(@id,'_close')]")).click();
      
      driver.findElement(By.xpath("//span[contains(@id,'_close') and contains(.,'wwwww')]")).click();
      
      driver.find_element_by_css_selector("span[id$='_close']").click();
      
      driver.find_element_by_css_selector("span[id*='_close']").click();
      
  • 或者,您也可以使用CssSelector,如下所示:

    • css\u选择器
      id
      属性使用ends with(即
      $
      通配符)子句:

      driver.findElement(By.xpath("//span[contains(@id,'_close')]")).click();
      
      driver.findElement(By.xpath("//span[contains(@id,'_close') and contains(.,'wwwww')]")).click();
      
      driver.find_element_by_css_selector("span[id$='_close']").click();
      
      driver.find_element_by_css_selector("span[id*='_close']").click();
      
    • css\u选择器
      id
      属性使用contains(即
      *
      通配符)子句:

      driver.findElement(By.xpath("//span[contains(@id,'_close')]")).click();
      
      driver.findElement(By.xpath("//span[contains(@id,'_close') and contains(.,'wwwww')]")).click();
      
      driver.find_element_by_css_selector("span[id$='_close']").click();
      
      driver.find_element_by_css_selector("span[id*='_close']").click();
      

您可以使用CSS选择器应用端点:

By.cssSelector("[id$=_close]")

css选择器搜索中也不需要包含span标记。

您可以共享HTML示例和所需的输出吗?示例wwwww,“xxxxx”是随机的。如果页面上有
Close
,您仍然认为不需要指定标记名吗?还要注意,您的代码不是Pythonoh yes。我的代码是特定于Java的。如果在这种情况下存在关闭,那么你可以根据你需要的条件去寻找过滤元素。只要没有其他代码元素中间的“<代码> >点击/代码>,这将起作用。虽然它很可能不太可能发生,但是如果它发生的话,它可能是一个非常讨厌的bug……”