Home > database >  Python Selenium - Please help me figure out why the button is not being clicked
Python Selenium - Please help me figure out why the button is not being clicked

Time:10-06

Good evening,

Please refer to the following webpage: https://prolineplus.olg.ca/en-ca/event/?e177405-Basketball-NBA-USA-Miami-Heat-Chicago-Bulls

I have two functions to click on either the Chicago bulls or Miami Heat as seen on the webpage from the link above. Can you please help me figure out why my current code is no longer clicking on either? I need to be able to use the team variable as the names will constantly change.

I greatly appreciate your help and will upvote/credit those who contribute to the solution!

Main:

driver = startup_login('https://prolineplus.olg.ca/en-ca/event/?e177405-Basketball-NBA-USA-Miami-Heat-Chicago-Bulls')

team = "Chicago"
proline_go_to_match2(driver,team)

Functions:

def proline_go_to_match2(driver, team):
    #print(team)
    try:
        
        match = WebDriverWait(driver, 15).until(
            EC.presence_of_element_located((By.XPATH, "//ul[@class='fdjgs-markets']/li[@class='fdjgs-market']//ul[@class='fdjgs-outcomes']//descendant::li[1]//span[@class='fdjgs-outcome-wrapper' and contains(@aria-label, '"  team  "')]/span[starts-with(., '"  team  "')]"))        )
        match.click()
    except:
        driver.quit()


def proline_go_to_match3(driver, team):
    #print(team)
    try:
        
        match = WebDriverWait(driver, 15).until(
            EC.presence_of_element_located((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, '"  team  "')]/span[starts-with(., '"  team  "')]"))        )
        match.click()
    except:
        driver.quit()

CodePudding user response:

Check for consent and click on that if exists. use python format() function to change the parameter. Use js executor to click else will get element intercepted error you can use actionchain as well.

driver.get("https://prolineplus.olg.ca/en-ca/event/?e177405-Basketball-NBA-USA-Miami-Heat-Chicago-Bulls")
wait=WebDriverWait(driver,20)

#check for consent
try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Continue']"))).click()
except:
    pass
team="Chicago"
items=wait.until(EC.visibility_of_all_elements_located((By.XPATH, "//span[contains(@aria-label, '{0}')]//span[starts-with(., '{0}')]".format(team))))

for item in items:
    driver.execute_script("arguments[0].click();", item)
    print("Button clicked")

CodePudding user response:

for your xpath, it can find two elements, you can try the following code, it can work well, opening browser will implicit to wait for page ready.

from clicknium import clicknium as cc

if not cc.chrome.extension.is_installed():
    cc.chrome.extension.install_or_update()

def get_elements(tab,team):
    xpath = "//ul[@class='fdjgs-markets']/li[@class='fdjgs-market']//ul[@class='fdjgs-outcomes']//descendant::li//span[@class='fdjgs-outcome-wrapper' and contains(@aria-label, '"  team  "')]/span[starts-with(., '"  team  "')]"
    return tab.find_elements_by_xpath(xpath)

tab = cc.chrome.open("https://prolineplus.olg.ca/en-ca/event/?e177405-Basketball-NBA-USA-Miami-Heat-Chicago-Bulls")
elements = get_elements(tab, "Chicago")
for elem in elements:
    elem.click()

elements = get_elements(tab, "Miami")
for elem in elements:
    elem.click()

CodePudding user response:

You have to add some wait time in the function and you don't need to use 2 functions to do click operations on 2 different team names, you can use just one, like below:

def proline_go_to_match(driver, team):
    try:
        time.sleep(4)
        match = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH,"(.//span[@class='fdjgs-outcome-wrapper' and @title=contains(.,'"   team   "')])[1]")))
        match.click()
    except:
        driver.quit()

Always, try to use simplified Xpath.

You can pass any value to the argument - 'team' like "Chicago" or "Miami".

  • Related