我正在研究Java Selenium-WebDriver.我补充道
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
和
WebElement textbox = driver.findElement(By.id("textbox"));
Run Code Online (Sandbox Code Playgroud)
因为我的应用程序需要几秒钟来加载用户界面.所以我设置2秒implicitwait.但我无法找到元素文本框
然后我补充说 Thread.sleep(2000);
现在它工作正常.哪一种更好?
我是Selenium WebDriver的新手,我正在努力理解"等待"元素出现的正确方法.
我正在测试一个包含一系列带有单选按钮答案的问题的页面.当您选择答案时,Javascript可能会启用/禁用页面上的一些问题.
问题似乎是Selenium"点击太快"而不是等待Javascript完成.我试过用两种方法解决这个问题 - 显式等待解决了这个问题.具体来说,这是有效的,并解决了我的问题:
private static WebElement findElement(final WebDriver driver, final By locator, final int timeoutSeconds) {
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(timeoutSeconds, TimeUnit.SECONDS)
.pollingEvery(500, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
return wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver webDriver) {
return driver.findElement(locator);
}
});
}
Run Code Online (Sandbox Code Playgroud)
但是,我宁愿使用隐式等待而不是这样.我的网页驱动程序配置如下:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
这没有解决问题,我得到一个NoSuchElementException.另外,我没有注意到10秒的暂停 - 它只是立即出错.我已经验证了代码中的这一行是用调试器命中的.我究竟做错了什么?为什么implicitlyWait不等待元素出现,但FluentWait呢?
注意:正如我所提到的,我已经有了解决方法,我真的只想知道为什么隐式等待不能解决我的问题.谢谢.
请分享有关selenium webdriver的知识.请显示实时示例.
我正在阅读SeleniumHQ文档并遇到以下声明.
"警告:不要混合隐式和显式等待.这样做可能会导致不可预测的等待时间.例如,设置10秒的隐式等待和15秒的显式等待可能会导致20秒后发生超时."
出于某种原因,我无法理解这一点.总超时20秒是我的主要困惑点.任何人都可以解释我是否遗漏了什么?
编辑
我的问题不是关于混合这些等待的实施/后果.它完全是关于doc上的语句和超时计算.
第二次编辑
根据下面的测试,看起来文档是正确的.我仍然需要解释.
using System;
using System.Diagnostics;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace Test
{
[TestFixture]
public class Test
{
private IWebDriver _webDriver;
[Test]
public void ExplicitVsImplicitWaitTest()
{
_webDriver = new ChromeDriver();
_webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
_webDriver.Navigate().GoToUrl("https://www.google.com/");
_webDriver.Manage().Window.Maximize();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
try
{
//new WebDriverWait(_webDriver, TimeSpan.FromSeconds(15)).Until(
//ExpectedConditions.ElementExists(By.CssSelector("Should Fail")));
_webDriver.FindElement(By.CssSelector("Should Fail"));
}
catch ( NoSuchElementException exception)
//catch ( OpenQA.Selenium.WebDriverTimeoutException)
{
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
}
_webDriver.Quit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
时间秒:00:00:10.0167290
using System; …Run Code Online (Sandbox Code Playgroud) 我的两个场景 -
1)首先
@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 45) # Time greater than implicit
@wait.until {@driver.find_element(:tag_name => "body").text.include?("hey")}
Run Code Online (Sandbox Code Playgroud)
这给了驱动程序45秒搜索文本(这是预期的)
2)第二
@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 5) # Time less than implicit
@wait.until {@driver.find_element(:tag_name => "body").text.include?("hey")}
Run Code Online (Sandbox Code Playgroud)
这现在为驱动程序提供了30秒的搜索时间(不是预期的)
有没有办法让硒等待explicit等待时间而不是两者中的较大者?
注意 - 不是声明隐式等待时间不是一个选项,因为每次驱动程序无法找到某些内容时我都无法让selenium挂起.
使用Selenium版本30,windows,ff
根据selenium,隐式等待轮询DOM一段时间以查看元素是否显示.我的理解是它将轮询到指定的时间量,但如果之前显示了一个元素,那么它将继续而无需进一步等待.
http://seleniumhq.org/docs/04_webdriver_advanced.html
我有一个在大约13秒内运行的方法.当我将隐式等待设置为100秒时,需要213秒.
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
看来在这种方法中,我等了2次(每次100秒).将隐式等待设置为0或100不会影响我的方法.在这两种情况下,他们都正确完成.
我的问题是这个.我认为隐式等待等待元素显示的最短时间.这是正确的吗?或者我做错了什么?
此外,为什么它等待2次,显然不需要等待?(即使我将等待设置为0,我的方法也能正确完成)
我刚刚开始学习 Selenium,需要使用云中的 jenkins 机器验证登录网页,该机器没有 GUI。我设法在具有 UI 的系统上成功运行该脚本。但是,当我修改脚本以无头运行时,它无法显示无法定位元素。我的脚本如下:
#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
import time
import argparse
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1120, 550')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--allow-running-insecure-content')
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)
driver.implicitly_wait(5)
lhip = '13.14.15.16'
user = 'username'
paswd = 'password'
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--lh_ip', type=str, metavar='', default=lhip, help='Public IP of VM' )
parser.add_argument('-u', '--usr', type=str, metavar='', default=user, help='Username for VM')
parser.add_argument('-p', '--pwd', type=str, metavar='', default=paswd, help='Password for …Run Code Online (Sandbox Code Playgroud) 如何等待 driver.get(),因为我们使用 .get() 访问的 URL 是不知道的。并且可能需要未知的时间,所以我们必须给 diver.get() 30 秒的超时时间,然后如何给它。
以下是它的代码..
package org.openqa.selenium.example;
import java.util.List;
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyClass{
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
WebDriver driver = new HtmlUnitDriver();
// Go to the some websites
driver.get("http://www.abced.com/123456/asd.htm");
/*** Here we DONT get back the driver, so we need to Give Time out of 30 seconds**/
final List<WebElement> element1= driver.findElements(By.tagName("a"));
for (WebElement webElement : element1) {
String …Run Code Online (Sandbox Code Playgroud)