任何人都可以给我发送示例代码如何验证元素
在Selenium WebDrvier中使用Java
我需要检查一个页面对象集合,看看每个页面对象是否在其WebDriver上调用了quit().
我编写了以下方法来检查WebDriver的状态:
public static boolean hasQuit(WebDriver driver) {
try {
driver.getTitle();
return false;
} catch (SessionNotFoundException e) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是问题:我不喜欢抛出并捕获异常以发现布尔值的真实性,但似乎我没有选择,因为WebDriver API没有提供检查驱动程序是否已退出的方法.
所以我的问题是,是否有更好的方法来检查WebDriver是否已退出?
我在这里找到了一个类似的(也是更一般的)问题,但问题没有任何已经尝试过的代码,唯一的答案是在退出后总是将WebDriver设置为null(我不一定能控制) .
Chrome 59 删除了对https:// user:password@example.com网址的支持.
我有一个C#selenium测试需要在Windows上以" 无头 "模式使用Chrome版本60
ChromeOptions options = new ChromeOptions();
options.AddArgument("headless");
driver = new ChromeDriver(chrome, options);
Run Code Online (Sandbox Code Playgroud)
这是我试图在Windows上处理的SAML身份验证所需对话框:

基于这里给出的答案:如何使用Java处理Selenium WebDriver的身份验证弹出窗口我可以看到在FireFox中处理此问题的几种解决方法,但在无头模式下Chrome 60没有.
在访问测试中的URL之前,我尝试使用以下代码访问带有凭据的URL(没有凭据)但是看起来Chrome 60 存在错误.
goTo("http://user:password@localhost"); // Caches auth, but page itself is blocked
goTo("http://localhost"); // Uses cached auth, page renders fine
// Continue test as normal
Run Code Online (Sandbox Code Playgroud)
我可以看到Firefox中的以下代码处理身份验证,对话框永远不会弹出:
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "https://saml.domain.com");
profile.EnableNativeEvents = false;`
Run Code Online (Sandbox Code Playgroud)
我试过(第二种方法使用的AutoIt),以及适用于Chrome 60,但确实不是在Chrome 60工作的无头模式.
//Use AutoIt to …Run Code Online (Sandbox Code Playgroud) c# basic-authentication selenium-chromedriver selenium-webdriver
我试图熟悉新的ruby selenium-webdriver,因为它看起来比以前版本的selenium和随之而来的ruby驱动程序更直观.另外,我无法让旧的selenium在windows中使用ruby 1.9.1,所以我想我会寻找替代方案.到目前为止,我已经用我的脚本完成了这个:
require "selenium-webdriver"
driver = Selenium::WebDriver.for :firefox
driver.get "https://example.com"
element = driver.find_element(:name, 'username')
element.send_keys "mwolfe"
element = driver.find_element(:name, 'password')
element.send_keys "mypass"
driver.find_element(:id, "sign-in-button").click
driver.find_element(:id,"menu-link-my_profile_professional_info").click
driver.find_element(:id,"add_education_btn").click
country_select = driver.find_element(:name, "address_country")
Run Code Online (Sandbox Code Playgroud)
所以基本上我正在登录我的网站,并尝试在我的用户配置文件中添加一个教育条目.我有一个带选项的选择框的引用(在country_select变量中),现在我想选择一个给定值的选项..我没有看到如何在新客户端中执行此操作.我唯一能想到的是循环遍历所有选项,直到找到我想要的那个,然后调用execute_script: http:// selenium. googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html#execute_script-class_method 方法设置selectedIndex.
有没有其他方法可以做到这一点?在这里的java api for selenium 2.0/webdriver:http://seleniumhq.org/docs/09_webdriver.html 有一个这样做的例子
Select select = new Select(driver.findElement(By.xpath("//select")));
select.deselectAll();
select.selectByVisibleText("Edam");
Run Code Online (Sandbox Code Playgroud)
除非我遗漏了某些东西,否则ruby版本似乎没有这个功能.任何帮助,将不胜感激.
我正在测试用户单击删除按钮的UI,表条目消失.因此,我希望能够检查表条目不再存在.
我试过使用ExpectedConditions.not()反转ExpectedConditions.presenceOfElementLocated(),希望它意味着"期望不存在指定的元素".我的代码是这样的:
browser.navigate().to("http://stackoverflow.com");
new WebDriverWait(browser, 1).until(
ExpectedConditions.not(
ExpectedConditions.presenceOfElementLocated(By.id("foo"))));
Run Code Online (Sandbox Code Playgroud)
但是,我发现即使这样做,我也会得到TimeoutExpcetion一个NoSuchElementException说法,即元素"foo"不存在.当然,没有这样的元素是我想要的,但我不想抛出异常.
那么我怎么能等到一个元素不再存在?我希望一个不依赖于捕获异常的示例(如我所知),应该抛出异常以用于异常行为).
有没有办法通过Protractor或WebDriver关闭标签?
我问,因为虽然我知道如何以编程方式切换标签,但它没有将活动标签置于前台.我无法总是告诉我在SauceLabs上运行的E2E测试中发生了什么,因为当我查看屏幕投射时,它显示的是我导航离开的标签,而不是活动标签.
我错了吗?
it('should do something in the previous tab', function(done) {
browser.getAllWindowHandles().then(function (handles) {
browser.switchTo().window(handles[0]);
// do something
expect(something).toEqual(thisThing);
done();
});
});
Run Code Online (Sandbox Code Playgroud) System.addProperty("webdriver.chrome.driver", ".../chromedriver.exe"); 在实例化驱动程序之前,我遇到了一些似乎已解决问题的问题.
我没有运气,我仍然得到文件.../bin/Debug/chromedriver.exe不存在的错误.
有没有人有幸运行而不把它放在bin文件夹中?
示例代码:
System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"c:\path\to\driver\chromedriver.exe");
BrowserDriver = new ChromeDriver();
Run Code Online (Sandbox Code Playgroud) 我是Selenium的新手,我被困在这里:
我想使用Selenium WebDriver 获取下拉列表的选定标签或值,然后在控制台上打印.
我可以从下拉列表中选择任何值,但我无法检索所选值并将其打印出来.
Select select = new
Select(driver.findElement(By.id("MyDropDown"))).selectByVisibleText(data[11].substring(1 , data[11].length()-1));
WebElement option = select.getFirstSelectedOption();
Run Code Online (Sandbox Code Playgroud)
但我所有的努力都是徒劳无益任何帮助都将受到赞赏.提前致谢 :)
我在TestNG中使用Selenium web Driver编写测试用例,我有一个场景,我按顺序运行多个测试(参见下文)
@Test(priority =1)
public void Test1(){
}
@Test(priority =2)
public void Test2(){
}
Run Code Online (Sandbox Code Playgroud)
两个测试都是AJAX调用,其中打开一个对话框,执行测试,然后关闭对话框,然后在页面顶部显示一条通知消息,并在每次成功测试后刷新页面.
问题是: Test2不等待页面刷新/重新加载.假设当Test1成功完成时,Test2将在页面刷新之前启动(即打开对话框,执行场景等).同时页面刷新(在成功执行Test1后必然会发生).现在,由于页面刷新,由于Test2打开的对话框不再存在,然后Test2失败.
(另外,我不知道刷新页面需要多长时间.因此,我不想在执行Test2之前使用Thread.sleep(xxxx))
另外,我不认为
driver.navigate().refresh()
Run Code Online (Sandbox Code Playgroud)
将它放在Test2之前将解决我的问题,因为在这种情况下我的页面将刷新两次.问题是通过代码进行刷新(不确定因为可能需要1秒或3秒或5秒)
在Selenium Grid中我试图执行一个简单的程序,Cannot find firefox binary in PATH虽然我已经在我的代码中添加了二进制路径.
我的代码和错误如下.请帮忙.提前致谢.
码
package Sample;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class sample1 {
WebDriver driver;
String BaseURL,NodeURL;
@BeforeTest
public void beforeTest() throws MalformedURLException {
BaseURL="www.google.com";
NodeURL="http://192.168.10.162:5566/wd/hub";
DesiredCapabilities capa =DesiredCapabilities.firefox();
capa.setBrowserName("firefox");
capa.setCapability("binary", "C:\\Users\\praveenraj.d\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
capa.setPlatform(Platform.ANY);
driver=new RemoteWebDriver(new URL(NodeURL),capa);
}
@Test
public void f() throws InterruptedException {
driver.get(BaseURL);
System.out.println(driver.getTitle());
Thread.sleep(50000);
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
Run Code Online (Sandbox Code Playgroud)
错误 …