我可以让 selenium 暂停输入并在触发器上恢复吗?
Can I make selenium pause for input and resume on a trigger?
我想知道selenium是否可以做下面的事情?我只想 自动化流程的某些部分:
- 加载网页(使用 angular 构建),提交带有一些内容的表单
预定义输入
- 在下一页,自动填写一些
数据和之前一样,但等我填写一些具体的输入
输入字段(不能硬编码此数据)
- 在此之后,触发器(如按钮
按或组合键;在网页之外)应该继续进行其余的自动流程和登陆
在第 3 页和第 4 页等等。
我熟悉的唯一选择是编写和 运行 修改表单元素的自定义 JS,在浏览器>检查>控制台中。对于以上内容,我必须在每个页面上 运行 不同的功能。为此,我可以注释掉除必需的函数调用和 运行 之外的所有内容。我想我不能从控制台 select 和 运行 只有一部分代码(例如第 1 页)。
PS:如果任何严格的 SO 人认为这不适合 SO,还有什么地方可以(以自动化为中心?)寻求为这类东西找到合适工具的好地方?
selenium 中有几个可用的等待。
Implicit Wait: During Implicit wait if the Web Driver cannot find it immediately because of its availability, it will keep polling (around 250 milli seconds) the DOM to get the element. If the element is not available within the specified Time an NoSuchElementException will be raised. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.
Explicit Wait: There can be instance when a particular element takes
more than a minute to load. In that case you definitely not like to
set a huge time to Implicit wait, as if you do this your browser will
going to wait for the same time for every element.
To avoid that situation you can simply put a separate time on the
required element only. By following this your browser implicit wait
time would be short for every element and it would be large for
specific element.
Fluent Wait: Let’s say you have an element which sometime appears in
just 1 second and some time it takes minutes to appear. In that case
it is better to use fluent wait, as this will try to find element
again and again until it find it or until the final timer runs out.
https://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/
注意:我通过 python 使用了 selenium,所以解决方案反映了这一点。
哦对了。这只是一个 python 脚本。不要用硒脚本来考虑它。 python 脚本可以轻松地等待输入。
print("Hi!. Script Started")
# code to load webpage, automatically fill whatever can be entered
x = input("Waiting for manual date to be entered. Enter YES when done.")
# Enter the data on page manually. Then come back to terminal and type YES and then press enter.
if x == 'YES':
continue_script_here()
else:
kill_script_or_something_else()
选项 1:
这可以使用显式等待轻松实现。假设您想在某个字段中手动输入数据。您可以让 selenium 等到字段包含值的点(它的 "value" 属性不为空)。例如:
WebDriverWait wait = new WebDriverWait(driver, 100);
//whatever time you think is sufficient for manually entering the data.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));
if(ExpectedConditions.attributeToBeNotEmpty(element,"value"))
{
//continue with the automation flow
}
选项 2:
有点老套。你可以做的是,在执行开始时打开另一个选项卡,然后切换回原来的选项卡,如下所示:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
driver.switchTo().defaultContent();
现在将开始执行,在您希望脚本停止以手动输入数据的位置,在无限循环中从 selenium 中检索所有选项卡,就像这样-
for(int i =1;i>0;i++)
{
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
if(tabs.size()==2)
{
//keep checking for the point when the number of tabs becomes 1 again.
continue;
}
else
{
break;
}
}
//your rest of the automation code
想法是让 selenium 暂停执行(因为它会卡在循环中)直到选项卡数量再次变为 1 的点。在此期间,您输入数据并关闭空选项卡,以便selenium 可以继续执行。
您也可以尝试this。
我想知道selenium是否可以做下面的事情?我只想 自动化流程的某些部分:
- 加载网页(使用 angular 构建),提交带有一些内容的表单 预定义输入
- 在下一页,自动填写一些 数据和之前一样,但等我填写一些具体的输入 输入字段(不能硬编码此数据)
- 在此之后,触发器(如按钮 按或组合键;在网页之外)应该继续进行其余的自动流程和登陆 在第 3 页和第 4 页等等。
我熟悉的唯一选择是编写和 运行 修改表单元素的自定义 JS,在浏览器>检查>控制台中。对于以上内容,我必须在每个页面上 运行 不同的功能。为此,我可以注释掉除必需的函数调用和 运行 之外的所有内容。我想我不能从控制台 select 和 运行 只有一部分代码(例如第 1 页)。
PS:如果任何严格的 SO 人认为这不适合 SO,还有什么地方可以(以自动化为中心?)寻求为这类东西找到合适工具的好地方?
selenium 中有几个可用的等待。
Implicit Wait: During Implicit wait if the Web Driver cannot find it immediately because of its availability, it will keep polling (around 250 milli seconds) the DOM to get the element. If the element is not available within the specified Time an NoSuchElementException will be raised. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.
Explicit Wait: There can be instance when a particular element takes more than a minute to load. In that case you definitely not like to set a huge time to Implicit wait, as if you do this your browser will going to wait for the same time for every element.
To avoid that situation you can simply put a separate time on the required element only. By following this your browser implicit wait time would be short for every element and it would be large for specific element.
Fluent Wait: Let’s say you have an element which sometime appears in just 1 second and some time it takes minutes to appear. In that case it is better to use fluent wait, as this will try to find element again and again until it find it or until the final timer runs out.
https://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/
注意:我通过 python 使用了 selenium,所以解决方案反映了这一点。
哦对了。这只是一个 python 脚本。不要用硒脚本来考虑它。 python 脚本可以轻松地等待输入。
print("Hi!. Script Started")
# code to load webpage, automatically fill whatever can be entered
x = input("Waiting for manual date to be entered. Enter YES when done.")
# Enter the data on page manually. Then come back to terminal and type YES and then press enter.
if x == 'YES':
continue_script_here()
else:
kill_script_or_something_else()
选项 1:
这可以使用显式等待轻松实现。假设您想在某个字段中手动输入数据。您可以让 selenium 等到字段包含值的点(它的 "value" 属性不为空)。例如:
WebDriverWait wait = new WebDriverWait(driver, 100);
//whatever time you think is sufficient for manually entering the data.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));
if(ExpectedConditions.attributeToBeNotEmpty(element,"value"))
{
//continue with the automation flow
}
选项 2:
有点老套。你可以做的是,在执行开始时打开另一个选项卡,然后切换回原来的选项卡,如下所示:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
driver.switchTo().defaultContent();
现在将开始执行,在您希望脚本停止以手动输入数据的位置,在无限循环中从 selenium 中检索所有选项卡,就像这样-
for(int i =1;i>0;i++)
{
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
if(tabs.size()==2)
{
//keep checking for the point when the number of tabs becomes 1 again.
continue;
}
else
{
break;
}
}
//your rest of the automation code
想法是让 selenium 暂停执行(因为它会卡在循环中)直到选项卡数量再次变为 1 的点。在此期间,您输入数据并关闭空选项卡,以便selenium 可以继续执行。
您也可以尝试this。