【问题标题】:How to use selenium on websites which use ajax?如何在使用 ajax 的网站上使用 selenium?
【发布时间】:2021-03-21 17:39:48
【问题描述】:

我曾尝试使用 selenium 自动化网站并从下拉菜单中选择一个选项,但我面临的问题是选择该选项后,ajax 未执行。这只发生在使用 selenium 时,我已经手动尝试过并且它有效。 Webdriver 等待对我不起作用。这是执行此操作的代码:

WebElement element =(driver.findElement(By.id("equity_optionchain_select")));
    Select elementSelect=new Select(element);
    elementSelect.selectByVisibleText("BANKNIFTY");

【问题讨论】:

  • @Verity 我的问题是我可以从该下拉菜单中选择值,但 ajax 未执行或 dom 未更改,因此选择后我无法看到差异值,当我使用没有 selenium 的网站时差异是可见的,这意味着 dom 发生变化,我希望它在使用 selenium 时更改 dom。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

BANKNIFTY而言,大概这些元素将是动态元素,即JavaScriptReactJSjQueryAjaxVue.jsEmber.jsGWT 等基于元素。所以理想情况下,您需要为elementToBeClickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

  • 使用 idselectByVisibleText():

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("equity_optionchain_select")))).selectByVisibleText("BANKNIFTY");
    
  • 使用 cssSelectorselectByVisibleText():

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#equity_optionchain_select")))).selectByVisibleText("BANKNIFTY");
    
  • 使用 xpathselectByVisibleText():

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='equity_optionchain_select']")))).selectByValue("BANKNIFTY");
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 2020-04-07
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多