Java 我们可以让 selenium webdriver 在运行时等待用户点击网页链接而不使用隐式等待吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21036239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:06:46  来源:igfitidea点击:

Can we make selenium webdriver to wait until user clicks on a webpage link at run-time without using implicit wait?

javaseleniumselenium-webdriver

提问by aks

I am using Firefox driver with java and trying to scrape some data from a website. There is a human interaction involved in it and I have to ask the user to input a search string. And then accordingly user has to select which search result to open by analyzing with human eye. The effort is just to make few bits and pieces work faster through script.

我正在使用带有 Java 的 Firefox 驱动程序并试图从网站上抓取一些数据。其中涉及人工交互,我必须要求用户输入搜索字符串。然后相应地用户必须通过人眼分析来选择要打开的搜索结果。努力只是通过脚本使一些零碎的工作更快。

My question is:

我的问题是:

Can we make selenium webdriver to wait until user clicks on a webpage link at run-time without using implicit wait? I can not use implicit wait because the time for click may vary from few seconds to few minutes.

我们可以让 selenium webdriver 在运行时等待用户点击网页链接而不使用隐式等待吗?我不能使用隐式等待,因为点击时间可能从几秒到几分钟不等。

I am new to both java and selenium. Your help will be much appreciated.

我对 java 和 selenium 都是新手。您的帮助将不胜感激。

- Thanks

- 谢谢

采纳答案by Amith

Selenium webdriver is a tool used to Automate tests on web based applications,which means even human interactions are automated.

Selenium webdriver 是一种用于对基于 Web 的应用程序进行自动化测试的工具,这意味着即使是人工交互也是自动化的。

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

隐式等待是告诉 WebDriver 在尝试查找一个或多个元素(如果它们不是立即可用)时轮询 DOM 一段时间。

This means ,you cannot use this to wait for human interactions.But you can assume that the user is waiting for some link to appear and call click().

这意味着,您不能使用它来等待人机交互。但是您可以假设用户正在等待某个链接出现并调用 click()。

This can be achieved using Explicit wait.

这可以使用显式等待来实现。

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.

显式等待是您定义的代码,用于在进一步处理代码之前等待特定条件发生。

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("some_link")));
element.click();

The above code waits for 30 secs for the link to appear and to be clickable.

上面的代码等待 30 秒链接出现并可以点击。

Refer this for more info.

有关详细信息,请参阅此处

回答by Grooveek

For sure you can. We use Selenium a lot as an assistant in getting some datas from websites.

你肯定可以。我们经常使用 Selenium 作为从网站获取一些数据的助手。

When you have a process involving multiple steps, including opening website, login, choosing 54 parameters and need only 1 human interaction (resolving a captcha, for example), Selenium can automate those boring tasks and let the user concentrate on the one which is important.

当您的流程涉及多个步骤,包括打开网站、登录、选择 54 个参数并且只需要 1 个人工交互(例如解析验证码)时,Selenium 可以自动化这些无聊的任务,让用户专注于重要的任务.

What we do here, is to make a Swing dialog with an input field to get what the user want to enter, then the actual form submission is performed by Selenium. You can do the same thing with links, and you can ask the user to select a link to click on, and perform the actual click with webdriver.

我们在这里所做的,是制作一个带有输入字段的 Swing 对话框来获取用户想要输入的内容,然后由 Selenium 执行实际的表单提交。你可以用链接做同样的事情,你可以让用户选择一个链接来点击,然后用 webdriver 执行实际的点击。

The ExplicitWaitmethod of Amithis also a good one. You can focus on what you expect after the user has clicked, and wait until this condition is realized

ExplicitWait方法Amith也是不错的。你可以在用户点击后专注于你所期望的,并等到这个条件实现

回答by Anthony L.

I ran into a similar issue where the program needs to be paused half way through for the human user to enter some 2-step verification information manually. What you can do is to show a message box at the human step. I work in Python, and here is what my code looks like:

我遇到了一个类似的问题,程序需要中途暂停,以便人类用户手动输入一些两步验证信息。您可以做的是在人工步骤中显示一个消息框。我在 Python 中工作,这是我的代码的样子:

from tkinter import messagebox
messagebox.showinfo(title='2-step verification', message='Finish on screen 2-step verification, and then click OK.')

Once the user finish the human step, he/she can click OK, and the rest of the program will run.

用户完成人工步骤后,他/她可以单击“确定”,程序的其余部分将运行。