0

I look up the information that lxml does not support xpath2.0 so that it can't use ends-with, so selenium can't use ends-with how to use it or replace ends-with. thank you very much indeed!!!

HTML sample

<span id="xxxxx_close">wwwww</span>

The 'xxxxx' part of @id is random

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
jianyi
  • 151
  • 1
  • 3
  • 11

2 Answers2

1

You can apply an ends-with CSS selector:

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

There's no need of including span tag in css selector search as well.

  • In case there is a `` on page you still think that there is no need to specify tag name? Also note that your code is not a Python – Andersson Jul 17 '18 at 09:41
  • oh yes. my code is specific to Java. And if is present in that case you can always go for filtering elements based on the criteria that you need. – Karthikeyan R Jul 17 '18 at 09:48
0

The ends-with XPath Constraint Function is part of XPath v2.0 but as per the current implementation Selenium supports XPath v1.0.

As per the HTML you have shared to identify the element you can use either of the Locator Strategies:

  • XPath using contains():

    • xpath using contains for id attribute:

      driver.findElement(By.xpath("//span[contains(@id,'_close')]")).click();
      
    • xpath using contains for id and innerHTML attribute:

      driver.findElement(By.xpath("//span[contains(@id,'_close') and contains(.,'wwwww')]")).click();
      
  • Alternatively, you can also use CssSelector as follows:

    • css_selector using ends-with (i.e. $ wildcard) clause for id attribute:

      driver.find_element_by_css_selector("span[id$='_close']").click();
      
    • css_selector using contains (i.e. * wildcard) clause for id attribute:

      driver.find_element_by_css_selector("span[id*='_close']").click();
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352