Home > Enterprise >  Selenium can't find element by class name which needs to be clicked
Selenium can't find element by class name which needs to be clicked

Time:01-06

On this page:

enter image description here

enter image description here

I have tried this in a few different ways but it looks like selenium can't the button. What can be the problem with mij code?

url = 'https://finance.yahoo.com/quote/AAPL/balance-sheet?p=AAPL'
driver.get(url)
#   driver.find_element(By.CSS_SELECTOR,'expandPf Fz(s) Bd(0) C($linkColor) C($linkActiveColor):h Fw(500) D(n)--print Fl(end) Mt(5px)')#.click()
#   driver.find_element(By.CLASS_NAME,'expandPf Fz(s) Bd(0) C($linkColor) C($linkActiveColor):h Fw(500) D(n)--print Fl(end) Mt(5px)')#.click()
#   driver.find_element(By.CLASS_NAME,'expandPf Fz(s)')#.click()
    showmore_link = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'expandPf Fz(s) Bd(0) C($linkColor) C($linkActiveColor):h Fw(500) D(n)--print Fl(end) Mt(5px)')))
    showmore_link.click()

None of my options seem to work.

CodePudding user response:

First of all these expandPf Fz(s) Bd(0) C($linkColor) C($linkActiveColor):h Fw(500) D(n)--print Fl(end) Mt(5px) are multiple and complex class names while By.CLASS_NAME receives single class name value. To work with multiple class name values you can use XPath or CSS Selectors.
But the easiest way to do that is to use XPath.
This XPath works: //span[contains(text(),'Expand All')]
This is the complete code I used. It expands the area after closing the pop-up

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "https://finance.yahoo.com/quote/AAPL/balance-sheet?p=AAPL"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Close']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Expand All')]"))).click()

The result is:

enter image description here

  • Related