【发布时间】:2016-06-28 08:11:08
【问题描述】:
我正在尝试在 gmail 中打开邮件以验证“www.gmail.com”上的链接,并且已从“www.abc.com”完成注册,那么我们如何使用 selenium WebDriver 和关键字驱动来识别邮件框架。
我做了两种方法,一种方法包含所有带操作的关键字,另一种方法包含位置。
但无论何时执行,我都无法点击邮件打开,因为在 WebElement 中它会为空。
那么,谁能检查我是否正确声明了语法?或者为什么我没有得到价值?
代码如下:
private boolean operateWebDriver(String operation, String Locator,String value, String objectName) throws Exception {
boolean testCaseStep = false;
try {
System.out.println("Operation execution in progress");
WebElement temp = getElement(Locator, objectName);
//Trying to click on email.
if(operation.equalsIgnoreCase("Click_anElement")){
if(temp.equals(value)){
temp.click();
}
}
//Trying to verify email.
if (operation.equalsIgnoreCase("Verify_Email")) {
System.out.println("Verify--->" + temp);
if(temp.equals(value)){
System.out.println("Verified");
}
else {
System.out.println("Not Verified");
}
}
testCaseStep = true;
} catch (Exception e) {
System.out.println("Exception occurred operateWebDriver"+ e.getMessage());
// Take screenshot if any test-case is not working.
System.out.println("Taking Screen Shot");
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("./Screenshots/"+screenshotName+".png"));
System.out.println("Screenshot taken");
}
return testCaseStep;
}
public WebElement getElement(String locator, String objectName) throws Exception {
WebElement temp = null;
System.out.println("Locator-->" + locator);
if (locator.equalsIgnoreCase("id")) {
temp = driver.findElement(By.id(objectName));
}
else if (locator.equalsIgnoreCase("xpath")) {
temp = driver.findElement(By.xpath(objectName));
System.out.println("xpath temp ----->" + temp);
}
else if (locator.equalsIgnoreCase("xpath1")){
String row_xpath_end= "]";
for(int rowNum=1; rowNum<=10; rowNum++){
temp = driver.findElement(By.xpath(objectName+rowNum+row_xpath_end));
}
}
return temp;
}
错误:
Exception occurred operateWebDriverThe given selector /html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div/div[7]/div/div[1]/div[2]/div/table/tbody/tr[ is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression /html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div/div[7]/div/div[1]/div[2]/div/table/tbody/tr[ because of the following error:
[Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)" location: "file:///C:/DOCUME~1/Chayan/LOCALS~1/Temp/anonymous1831932369561342639webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 5934"]
Command duration or timeout: 16 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'aa-ff7b4a68', ip: '192.168.1.3', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_71'
Session ID: 051985f9-6320-4d0f-ad48-0ab3ff975f02
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=18.0}]
Taking Screen Shot
【问题讨论】:
-
是否输入
getElement()中的if选项之一? -
@Guy 实际上它无法在“temp = driver.findElement(By.xpath(objectName+rowNum+row_xpath_end));”行中找到元素。你能给它一些解决方案吗?我也发布了我得到的错误,请检查。
-
您提供的
xpath以tr[结尾,因此您收到错误"The expression is not a legal expression."。你确定它不会进入第二个if吗?else if (locator.equalsIgnoreCase("xpath")) -
@Guy 实际上它应该转到行 else if (locator.equalsIgnoreCase("xpath1")) 因为打开邮件的逻辑写在这里然后,它转到方法名称“operateWebDriver” . 在这些中获取值(邮件主题行),然后单击它。这是我正在尝试做的。
-
附带说明,依赖像
/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div/div[7]/div/div[1]/div[2]/div/table/tbody这样的xpath 是一个糟糕的主意。首先,页面上的任何细微变化都会被破坏,其次很难理解您要查找的内容等等。我会花时间寻找一些更符合逻辑的 xpath(例如,使用 id、class 一些属性,而不是 xpath 在页面上的位置)
标签: java selenium selenium-webdriver frameworks