英文:
Selenium:Both wait and get element when ready
问题
Instead of doing 2 steps:
等待直到元素准备好:
wait.until(webDriver -> webDriver.findElement(By.id("userTable")));
and then retrieving the element when it's ready:
然后在元素准备好后检索该元素:
WebElement x = webDriver.findElement(By.id("userTable"));
can this be done in one step?
这是否可以合并为一步操作?
For instance, I don't want to do:
例如,我不想这样做:
wait.until(webDriver -> webDriver.findElement(By.id("userTable")).findElement(By.xpath(".//*[@id="userTable"]/tbody/tr/td[1]/a"))).click();
但我想将其分解为几个步骤,因为这更清晰:
但是,我希望将其分解为几个步骤,因为这更清晰:
That is first wait for it to be ready:
首先等待元素准备好:
wait.until(webDriver -> webDriver.findElement(By.id("userTable")));
then get a reference to it:
然后获取对它的引用:
WebElement x = webDriver.findElement(By.id("userTable"));
and then get the child element:
然后获取子元素:
x.findElement(By.xpath(".//*[@id="userTable"]/tbody/tr/td[1]/a"))).click();
So can the wait and getting the reference parts be somehow joined in one step?
那么等待和获取引用的部分是否可以合并为一步?
英文:
Instead of doing 2 steps:
wait.until(webDriver -> webDriver.findElement(By.id("userTable")));
and then retrieving the element when it's ready:
WebElement x = webDriver.findElement(By.id("userTable"));
can this be done in one step?
For instance,I don't want to do :
wait.until(webDriver -> webDriver.findElement(By.id("userTable")).findElement(By.xpath(".//*[@id=\"userTable\"]/tbody/tr/td[1]/a"))).click();
but would like to break it down into steps because it's clearer:
That is first wait fo it to be ready:
wait.until(webDriver -> webDriver.findElement(By.id("userTable")));
then get a reference to it:
WebElement x = webDriver.findElement(By.id("userTable"));
and then get the child element:
x.findElement(By.xpath(".//*[@id=\"userTable\"]/tbody/tr/td[1]/a"))).click();
So can the wait and getting the reference parts be somehow joined in one step?
答案1
得分: 2
until
方法返回<T>
,这是一个泛型,根据你在lambda表达式中提供的返回类型。因为findElement
返回WebElement
,所以until
方法的返回类型也将是WebElement
。
通过提供的实现,你已经解决了问题,因为until
将返回WebElement
或抛出TimeoutException
。
只需将引用保存到变量中:
WebElement userTable = wait.until(webDriver -> webDriver.findElement(By.id("userTable")));
英文:
until
method returns <T>
which is generic and based on return type you provide in the lambda expression. Since findElement
returns WebElement
, the return type of until
method will also be WebElement
.
With provided implementation, you already solved your problem as until
will return WebElement
or throw TimeoutException
Just save the reference to a variable:
WebElement userTable = wait.until(webDriver -> webDriver.findElement(By.id("userTable")));
答案2
得分: 0
以下是已翻译的部分:
"As your usecase is to invoke click()
you can induce WebDriverWait inconjunction with ExpectedConditions for the elementToBeClickable()
and you can use either of the following Locator Strategies:
-
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#userTable>tbody>tr td:nth-child(2)>a"))).click();
-
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='userTable']/tbody/tr/td[1]/a"))).click();"
英文:
As your usecase is to invoke click()
you can induce WebDriverWait inconjunction with ExpectedConditions for the elementToBeClickable()
and you can use either of the following Locator Strategies:
-
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#userTable>tbody>tr td:nth-child(2)>a"))).click();
-
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='userTable']/tbody/tr/td[1]/a"))).click();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论