-1

I am using Selenium with Python to webscrape a page which includes JavaScript. The racecourse result tabs towards the top of the page eg "Ludlow","Dundalk" are manually clickable but do not have any obvious hyperlinks attached to them. ... from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path='C:/A38/chromedriver_win32/chromedriver.exe')

driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("https://www.sportinglife.com/racing/results/2020-11-23")

...

This works so far. I have used BeautifulSoup to find the label names of the NewGenericTabs eg "Ludlow","Dundalk" etc. However, the following code, to attempt to automate clicking a tab, times out each time.

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.LINK_TEXT, "Ludlow"))).click()

Would welcome any help.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user25236
  • 11
  • 5

1 Answers1

1

The WebElement aren't <a> tags but <span> tags so By.LINK_TEXT wouldn't work.

To click on the desired elements you can use either of the following based Locator Strategies:

  • Ludlow:

    driver.get("https://www.sportinglife.com/racing/results/2020-11-23")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@mode='primary']"))).click()
    driver.find_element_by_xpath("//span[@data-test-id='generic-tab' and text()='Ludlow']").click()
    
  • Dundalk:

    driver.get("https://www.sportinglife.com/racing/results/2020-11-23") 
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@mode='primary']"))).click()  
    driver.find_element_by_xpath("//span[@data-test-id='generic-tab' and text()='Dundalk']").click()
    

Ideally, to click on the elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following based Locator Strategies:

  • Ludlow:

    driver.get("https://www.sportinglife.com/racing/results/2020-11-23")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@mode='primary']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@data-test-id='generic-tab' and text()='Ludlow']"))).click()
    
  • Dundalk:

    driver.get("https://www.sportinglife.com/racing/results/2020-11-23") 
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@mode='primary']"))).click()  
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@data-test-id='generic-tab' and text()='Dundalk']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you. Selenium has now tried to click the tab but there is an error message: ElementClickInterceptedException: element click intercepted: Element ... is not clickable at point (576, 447). Other element would receive the click:
    ...
    (Session info: chrome=87.0.4280.88)
    – user25236 Dec 10 '20 at 11:22
  • @user25236 Checkout the updated answer and let me know the status. – undetected Selenium Dec 10 '20 at 11:44
  • Thank you. The first options worked individually but if I combined them sequentially in a script then it stopped at the Ludlow page and a timeout error was given (presumably trying for the Dundalk page). The second options did not work at all and gave a Chrome not reachable error. – user25236 Dec 11 '20 at 11:54
  • @user25236 Can you raise a new question as per your new requirement please? StackOverflow contributors will be happy to help you out. – undetected Selenium Dec 11 '20 at 11:56
  • Thank you for your great help so far. My original question was " code, to attempt to automate clicking a tab". So far, the answer only works for clicking the first tab chosen and not any others. – user25236 Dec 12 '20 at 13:44