0
quem_esta_sacando = driver.find_elements_by_xpath("//div[@class='gameinfo-container tennis-container']/div[@class='team-names']/div[@class='team-name']")

this is how i found the right class, but i wanna find the order of it

<div class="gameinfo-container tennis-container">
    <div class="team-names">
        <div class="team-name">
            <img alt="ball">
        <div class="team-name">
            <img alt="ball" class="active">

the last class (active) may change during the time it passes, so, sometimes its will be like the first, but sometimes it will be like this:

<div class="gameinfo-container tennis-container">
    <div class="team-names">
        <div class="team-name">
            <img alt="ball" class="active">
        <div class="team-name">
            <img alt="ball">

i wanna know, without looking every time, who come first

i try to use this:

if active in driver.find_elements_by_xpath("//div[@class='gameinfo-container tennis-container']/div[@class='team-names']/div[@class='team-name'][last()]").get_attribute("class"):
    print("last element")
else:
    print("Wasn't the last element")

but it's became that the name "active" isn't defined

1 Answers1

0

As per the probhable actual html:

<div class="gameinfo-container tennis-container">
    <div class="team-names">
    <div class="team-name">
        <img alt="ball">
    <div class="team-name">
        <img alt="ball" class="active">

You can probe the last matching WebElement, if it's outerHTML or class contains the value active and you can use either of the following approaches:

  • Probing the outerHTML:

    if active in driver.find_element_by_xpath("///div[@class='gameinfo-container tennis-container']/div[@class='team-names']//div[@class='team-name'][last()]/img[@alt='ball']").get_attribute("outerHTML"):
        print("last element")
    else:
        print("Wasn't the last element")
    
  • Probing the classname:

    if active in driver.find_element_by_xpath("//div[@class='gameinfo-container tennis-container']/div[@class='team-names']//div[@class='team-name'][last()]/img[@alt='ball']").get_attribute("class"):
        print("last element")
    else:
        print("Wasn't the last element")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i think it is right, all the logic make sense, but when i try to run it, thats the error becames to me: global name 'active' is not defined – caleul raposo caixeta Jul 27 '20 at 13:48
  • @caleulraposocaixeta My bad, I posted an untested solution as the html you provided was syntactically not 100% correct and I wanted to preserve your working [Locator Strategies](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890). Updated the answer now, let me know the status. – undetected Selenium Jul 27 '20 at 14:33