1

""" Web scraping the wikipidia page """

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.get("https://en.wikipedia.org/wiki/Main_Page")

num_articles1 = driver.find_element(By.CSS_SELECTOR, '#articlecount a')
print(num_articles1.text)

num_articles1.click()


driver.close()

Question: num_articles1 returns the value but why is the click() not working ? can't understand why is this happening, what am i missing?

A.saini
  • 11
  • 1

1 Answers1

0

On my Windows 10 using latest Selenium, ChromeDriver and Chrome the number of articles i.e. 6,559,615 gets printed perfectly and the click() is also performed perfecto.


However, to click on the clickable element ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#articlecount a"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='articlecount']/a"))).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
  • Yeah, it is a working code, just need to add a while loop to keep it working using the date time module – A.saini Aug 03 '22 at 03:44