0
votes

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.

1

1 Answers

0
votes

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