1

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>
jwalman
  • 245
  • 2
  • 8
  • 2
    You're looking for the substring `INDIANA`, but the actual text contains `Indiana`. – Paul M. Dec 29 '21 at 18:49
  • @PaulM. thank you very much! I cant believed I overlooked that, your solution worked! Have a great day. – jwalman Dec 29 '21 at 18:52
  • @PaulM. Hi Paul, with the change the code did work however it is selecting the button/link I do not intend for it to select. I have updated my question can you please take a look to see? – jwalman Dec 29 '21 at 19:11
  • @jwalman can you share a link to that page or at least the HTML of it? In order to create correct locator we need to see the entire page, not just a single element there – Prophet Dec 29 '21 at 19:32
  • @Prophet Hi, thanks for taking a look! I have added a link to the page. I also updated the code. What is shown will click the second "Indiana" box and not "Indiana -4.5" – jwalman Dec 29 '21 at 19:39

2 Answers2

2

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thank you for the help! However my issue is that the only variable I will be using is "Indiana" and I want to select the link/button "Indiana -4.5" however my code is currently selecting a different button/link. Please not that the "-4.5" is unknown. It could be "+" OR "-" followed by some one or two digit decimal number. – jwalman Dec 29 '21 at 19:41
  • 1
    @jwalman Checkout the updated answer and let me know the status. – undetected Selenium Dec 29 '21 at 20:00
  • Thank you!! It looks like that works. How would I replace 'Indiana' in your code with the variable team? – jwalman Dec 29 '21 at 20:04
  • 1
    @jwalman It shouldn't look like rather it should ;) code is well tested – undetected Selenium Dec 29 '21 at 20:06
  • I have tested it and it works! Just looking to add Indiana as a variable named team rather hardcode in Indiana. Much appreciated! – jwalman Dec 29 '21 at 20:08
  • 1
    @jwalman Can you raise a new question as per your new requirement please? – undetected Selenium Dec 29 '21 at 20:11
1

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()
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Thank you for the response! Can you please help understand the difference between visibility and presence and why I should use presence instead? Much appreciated! – jwalman Dec 29 '21 at 20:13
  • As literally meant by their names, `presence_of_element_located` wait for the presence of such element on the page. It takes some time to complete element rendering on the page. So on it's early stages of rendering the element will already exist on the page however it may still be not clickable, still not contain its text, it may not be located on it final location on the page etc. However when element is visible it normally indicates the element is completed, it contains it text, can be clicked etc. – Prophet Dec 29 '21 at 20:25
  • So we should always prefer using `visibility_of_element_located` until we know the element is not visible but we still need to wait for it's presence for some reason. For example if this element is out of the visible screen view. – Prophet Dec 29 '21 at 20:27
  • Hey is there any way I can add a condition to only look at elements that are displayed on the screen and currently visible? The code you sent seems to only work part of the time. Sometimes it is clicking on the wrong button at the very bottom of the page which I dont want to happen. – jwalman Dec 29 '21 at 23:18
  • I'm not sure this will work correctly since element visibility defined by Selenium will not always equally to human meaning of that. Element may be covered by some other element or be out of the actual screen view but it still can be recognized as visible by Selenium. I think we have to define the locator more accurate or make more precise logic of out code to achieve what you need here. – Prophet Dec 30 '21 at 07:44