【问题标题】:Press button using selenium on yahoo finance doesn't work在雅虎财经上使用硒按下按钮不起作用
【发布时间】:2020-12-11 20:50:58
【问题描述】:

我想获得当天的热门股票,所以我去了https://finance.yahoo.com/gainers,但我想通过按编辑来编辑过滤器。

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()

这不起作用。我该如何解决?

【问题讨论】:

  • 使用 data-reactid... xpath of "//span[@data-reactid='23']"

标签: python-3.x selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

要点击元素Edit,您可以使用以下任一Locator Strategies

  • 使用xpath

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

理想情况下,点击您需要为WebDriverWait 诱导element_to_be_clickable() 的元素,您可以使用以下任一Locator Strategies

  • 使用XPATH

    driver.get("https://finance.yahoo.com/gainers")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Edit']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

【讨论】:

    【解决方案2】:

    下面的 java 代码似乎可以工作。

            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();
    

    清洁器定位器

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 2023-03-27
      相关资源
      最近更新 更多