0

I have two problems: first, I cannot click on the show all bottom; and, second, I cannot get the data from the high chart.

I saw some examples for the high chart on Stack Overflow; however, I did not get how people figure our which JS code to execute.

I tried the following code to achieve that:

from selenium import webdriver
DRIVER_PATH = r"C:\Users\XX\Downloads\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
url = "https://siterankdata.com/wsj.com"
driver.get(url)
driver.find_element_by_xpath('//*[@id="smallchart"]/div/div/svg/g[17]/g/text/tspan').click() # Does not work I try to click on the show all button.

I would appreciate any help!

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Dogukan Yılmaz
  • 556
  • 2
  • 15

2 Answers2

1

To click on show all button Use WebDriverWait() and wait for element_to_be_clickable() and following xpath

driver.get(url)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@id='smallchart']//*[name()='svg']/*[name()='g'][17]/*[name()='g']/*[name()='text']/*[name()='tspan']"))).click()

You need to import below libraries

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
KunduK
  • 32,888
  • 5
  • 17
  • 41
1

The element with text as Show all is a element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://siterankdata.com/wsj.com")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#smallchart svg g text[text-anchor='start'] tspan"))).click()
    
  • Using XPATH:

    driver.get("https://siterankdata.com/wsj.com")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id="smallchart"]//*[name()='svg']//*[name()='g']//*[name()='text']//*[name()='tspan' and text()='Show all']"))).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
    

References

You can find a couple of relevant discussions on interacting with SVG element in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352