1

I am trying to get the top stocks for the day so I go to https://finance.yahoo.com/gainers but I the want to edit the filters by pressing Edit.

driver = webdriver.Chrome()
driver.get("https://finance.yahoo.com/gainers")
element = driver.find_element_by_class_name("Bgc($linkColor).Bgc($linkActiveColor):h.C(white).Fw(500).Px(20px).Py(9px).Bdrs(3px).Bd(0).Fz(s).D(ib).Whs(nw).Miw(110px)")
element.click()

This doesn't work. How can I fix it?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Justin Mu
  • 13
  • 2

2 Answers2

0

The java code below seems to work.

        WebDriver driver = new ChromeDriver();
        driver.get("https://finance.yahoo.com/gainers");
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 30);
        WebElement editButton = wait
                .until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[data-reactid=\"23\"]")));
        editButton.click();

Cleaner locator

        WebElement editButton = wait
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button/span[contains(text(),'Edit')]")));
Vinay
  • 71
  • 1
  • 4
0

To click on the element Edit you can use either of the following Locator Strategies:

  • Using xpath:

    driver.get("https://finance.yahoo.com/gainers")
    driver.find_element_by_xpath("//span[text()='Edit']").click()
    

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

  • Using XPATH:

    driver.get("https://finance.yahoo.com/gainers")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Edit']"))).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