如何在页面异步更改内容时创建一个 WebDriverWait(无需重新加载页面)?

huangapple go评论97阅读模式
英文:

How to make a WebDriverWait if page changes its content asynchronously (without page reloading)?

问题

// 使用Selenium Webdriver(Java)编写测试代码,将https://cloud.google.com作为驱动程序。
// 首先查找搜索输入字段,然后使用sendKeys("search phrase \n")发送搜索短语。
// 在此之后,页面开始更改其内容,我尝试使用WebDriverWait拦截这些更改:

// 第一个等待 - 等待页面开始通过删除搜索Google图标来更改其内容
new WebDriverWait(driver, 30).until(ExpectedConditions.invisibilityOf(searchInputFieldIcon));

// 第二个等待 - 我在等待新的超链接出现(在整个页面在无需重新加载的情况下异步重新加载后,此超链接将出现在搜索结果中)
new WebDriverWait(driver, 30)
    .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='https://cloud.google.com/products/calculator']")));

// 关键是,等待不会在元素显示之前等待30秒。代码只是抛出异常:

org.openqa.selenium.NoSuchElementException:
没有这样的元素无法定位元素{"method":"xpath","selector":"//a[@href='https://cloud.google.com/products/calculator']"}

非常感谢任何帮助
英文:

I'm coding test with Selenium Webdriver (Java), getting https://cloud.google.com as a driver.
I start with finding search input field, sendKeys("search phrase \n"). After that page starts changing its content and I'm trying to intersept these changes with WebDriverWait:

    // first Wait - is to wait before page starts changing is content by removing search google icon
            new WebDriverWait(driver, 30).until(ExpectedConditions.invisibilityOf(searchInputFieldIcon));
    //second Wait  - i'm waiting new hyperlink to appear (this hyperlink appears in search results after the whole page is asynchronically reloaded without page reloading)            
            new WebDriverWait(driver,30)
        .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='https://cloud.google.com/products/calculator']")));

The point is, that Wait doesn't waits for 30 seconds before element shows up. Code just throws an exception:

    org.openqa.selenium.NoSuchElementException:
 no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href='https:`//cloud.google.com/products/calculator']"}`

Any help will be much appreciated!

答案1

得分: 1

请查看附上的截图。这里的Href链接与您在代码中使用的不同。

您可以使用以下代码:

wait.until(expectedConditions.visibilityOfElementLocated(By.linkText("Google Cloud Platform Pricing ")));
英文:

如何在页面异步更改内容时创建一个 WebDriverWait(无需重新加载页面)?

Please check the attached screenshot. Here the Href link is different then you have used in your code.

You can use the below code

wait.until(expectedConditions.visibilityOfElementLocated(By.linkText("Google Cloud Platform Pricing ")));

</details>



# 答案2
**得分**: 0

以下是翻译好的内容:

要定位第一个搜索结果,您可以使用以下 xpath;

```xpath
//a[contains(text(),'Google Cloud Platform Pricing')]

检查您的 xpath

您可以从浏览器本身检查您的 xpath 是否正确。

  1. 进入开发者工具(Ctrl + Shift + I)
  2. 在“元素”选项卡中,按下 Ctrl + F
  3. 输入您要检查的 xpath

然后它将显示出该 xpath 是否正确,以及可以从中定位多少个网页元素。

如何在页面异步更改内容时创建一个 WebDriverWait(无需重新加载页面)?

英文:

To locate the first search result you can use the following xpath;

//a[contains(text(),&#39;Google Cloud Platform Pricing&#39;)]

Checking your xpath

You can check whether your xpath is correct or not from the browser itself.

  1. Go to DevTools (Ctril + Shift + I)
  2. In the 'Elements' tab, press Ctrl + F
  3. Input the xpath that you want to check

And it will show you whether it is correct and how many web-elements can be located from it.

如何在页面异步更改内容时创建一个 WebDriverWait(无需重新加载页面)?

huangapple
  • 本文由 发表于 2020年5月5日 19:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定