jwalman
jwalman

Reputation: 265

Python Selenium-WebDriver - How to Identify this Button/Link

Please refer to the screenshot below and the HTML code. There are two buttons/links that have "Indiana -4.5" and "Indiana" in their name. I am trying to identify the first that also has "-4.5" however I will not know what that number is as it changes. The name "Indiana" can be followed by a "+", "-" which is then followed by a one or two digit decimal number.

Given only "Indiana" as a variable how can I select the "Indiana -4.5" and not "Indiana" link/button. Thank you!

Link to page: https://prolineplus.olg.ca/en-ca/event/?e64240-NBA-Basketball-USA-Indiana-Charlotte

enter image description here

    def proline_go_to_match2(driver, team):
        team = "Indiana"
        print(team)
        try:
            match = WebDriverWait(driver, 15).until(
                EC.presence_of_element_located((By.XPATH, "//*[@title= '" + team +  "']"))
            )
            match.click()
        except:
        driver.quit()

I cannot figure out how to get Seleium to identify this link and would appreciate your help!!

Here is the HTML element

Here is the first link/button:

<span class="fdjgs-outcome-wrapper" aria-pressed="true" role="button" tabindex="0" aria-label="Bet for Charlotte @ Indiana - Indiana -4.5 at 2.10" title="Indiana -4.5"><span class="fdjgs-description">Indiana -4.5</span><span class="fdjgs-price">2.10</span></span>

Here is the second link/button:

<li class="fdjgs-outcome" title="Indiana" data-state="unselected" data-direction="" data-unpriced="false" data-hidden="false" data-resulted="false" data-suspended="false"><span class="fdjgs-outcome-wrapper" aria-pressed="false" role="button" tabindex="0" aria-label="Bet for Charlotte @ Indiana - Indiana at 1.65" title="Indiana"><span class="fdjgs-description">Indiana</span><span class="fdjgs-price">1.65</span></span></li>

Upvotes: 1

Views: 469

Answers (2)

Prophet
Prophet

Reputation: 33361

You should use visibility_of_element_located instead of presence_of_element_located.
With the correct locator your code can be as following:

def proline_go_to_match2(driver, team):
    team = "Indiana"
    print(team)
    try:
        match = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//header[.//span[text()='Point Spread'] and .//span[contains(.,'Match')]]/..//li[contains(@title,'" + team +  "')]"))            )
        match.click()
    except:
       driver.quit()

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193138

To click on the element Indiana -4.5 instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='fdjgs-markets']/li[@class='fdjgs-market']//ul[@class='fdjgs-outcomes']//descendant::li[2]//span[@class='fdjgs-outcome-wrapper' and contains(@aria-label, 'Indiana')]/span[starts-with(., 'Indiana')]"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

Indiana

Upvotes: 2

Related Questions