package com.test.webdriver;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestExample {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testExample() throws Exception {
driver.get(baseUrl + "/webhp?tab=ww&ei=PaDQU4j6N4-QuATW2oB4&ved=0CBMQ1S4");
driver.findElement(By.xpath("id=gbqfq")).sendKeys("Test");
driver.findElement(By.xpath("id=gbqfba")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
有人可以告诉我为什么上面的代码不起作用? 我在eclipse中运行上面的引用并得到以下错误
Eclipse版本:3.7.1 Firefox版本:30
答案 0 :(得分:1)
首先,您的xpath不正确,一旦您在文本框中输入内容,就会再次显示您尝试单击的按钮。
这将有效,
driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys("Test");
driver.findElement(By.xpath("//*[@id='gbqfb']/span")).click();
您使用的是其他按钮的错误路径。为什么呢?
bcoz,当你在google中输入内容时,你试图点击的按钮(Google按钮)将会消失,完全会出现一个新页面(你可以注意到文本框移动到右上角,然后是蓝色搜索图标将出现)。
因此请注意应用程序中发生的情况,然后尝试自动化它。
答案 1 :(得分:0)
您的xpath无效。请尝试:
“// * [@ id中= 'gbqfba']”
大多数浏览器都允许您测试Xpath。
或者,css选择器将是;
“#gbqfba”
或者Selenium也允许你通过id找到;
“gbqfba”
我个人更喜欢CSS选择器。您选择XPath的原因是什么?
答案 2 :(得分:0)
嗨,这应该工作了我在评论块中留下了一些评论 干杯..希望这有助于查看可能有助于定位器的链接
https://thenewcircle.com/static/bookshelf/selenium_tutorial/locators.html
http://www.w3schools.com/xpath/xpath_syntax.asp
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestExample {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}
@Test
public void testExample() throws Exception {
driver.get(baseUrl + "/webhp?tab=ww&ei=PaDQU4j6N4-QuATW2oB4&ved=0CBMQ1S4");
driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys("Test");
/**Comment
The element with id "gbqfba" becomes invisble as page nagivates to results/ suggestion page
instead a button with id "gbqfb" becomes available to click to submit
driver.findElement(By.xpath("//*[@id='gbqfba']")).click(); */
//I decided to submit whatever is entered into the SearchField here and everything passes
driver.findElement(By.xpath("//*[@id='gbqfq']")).submit();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}