我想点击下拉菜单,检查网页标题,检查每个链接的页面是否包含有效内容。
如果页面标题为“正在构建:Mercury Tours”,则该链接包含无效内容。否则,有效内容。
我的程序: 1.存储String数组“xpath”中所有有效链接的xpath。 2.遍历字符串数组并使用预定义的无效页面标题检查标题。
第1步很好,我可以看到所有的xpath都被打印出来了。问题出在第二步。代码无法遍历String数组并单击每个链接。请帮忙。
package pack1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.*;
import org.openqa.selenium.WebElement;
public class CheckEveryDropDownLink {
public static void main(String[] args) {
// TODO Auto-generated method stub
String baseURL = "http://demo.guru99.com/test/newtours/";
String underConsTitle = "Under Construction: Mercury Tours";
int size=0;
String[] xpath = new String[20];
String temp;
String tempxpath;
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(baseURL);
driver.findElement(By.xpath(".//*[@id='navbar-brand-
centered']/ul/li[1]/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
tempxpath=".//*[@id='navbar-brand-centered']/ul/li[1]/a";
for(int i=1,j=0;i<21 ;i++)
{
driver.findElement(By.xpath(".//*[@id='navbar-brand-
centered']/ul/li[1]/a")).click();
temp= ".//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[" + i + "]/a";
if(driver.findElement(By.xpath(temp)).isEnabled())
{
xpath[j] = temp;
System.out.println(xpath[j]);
j++;
}
}
for(int i=1;i<21;i++)
{
String xp=xpath[i];
driver.findElement(By.xpath(".//*[@id='navbar-brand-
centered']/ul/li[1]/a")).click();
driver.findElement(By.xpath(xp)).click();
if(driver.getTitle().equals(underConsTitle))
{
System.out.println("The page has invalid content");
}
else
{
System.out.println("The page has valid content");
}
driver.navigate().back();
}
}
}
输出结果为:
1520964562367 geckodriver INFO geckodriver 0.19.1
(0000000000000000000000000000000000000000 2017-10-31)
1520964562374 geckodriver INFO Listening on 127.0.0.1:32329
1520964562778 mozrunner::runner INFO Running command: "C:\\Program
Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile"
"C:\\Users\\user\\AppData\\Local\\Temp\\rust_mozprofile.RrB8rmJsgtnf"
1520964564749 Marionette INFO Enabled via --marionette
1520964570359 Marionette INFO Listening on port 28399
1520964570656 Marionette WARN TLS certificate errors will be ignored
for this session
Mar 13, 2018 11:39:30 PM org.openqa.selenium.remote.ProtocolHandshake
createSession
INFO: Detected dialect: W3C
JavaScript warning: https://www.googletagmanager.com/gtag/js?id=UA-1248015-
24, line 69: unreachable code after return statement
JavaScript warning: https://www.googletagmanager.com/gtag/js?id=UA-1248015-
24, line 69: unreachable code after return statement
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[1]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[2]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[3]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[4]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[5]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[6]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[7]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[8]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[9]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[10]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[11]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[12]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[13]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[14]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[15]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[16]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[17]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[18]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[19]/a
.//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[20]/a
Exception in thread "main"
org.openqa.selenium.ElementNotInteractableException: Element <a
href="../../test/radio.html"> could not be scrolled into view
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-
01T19:05:14.666Z'
System info: host: 'CHANDU', ip: '192.168.116.1', os.name: 'Windows 10',
os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox,
browserVersion: 58.0, javascriptEnabled: true, moz:accessibilityChecks:
false, moz:headless: false, moz:processID: 3132, moz:profile:
C:\Users\user\AppData\Local..., moz:webdriverClick: true, pageLoadStrategy:
normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable:
false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 6ebc7f47-3619-4e22-9a0f-79684bb7c5e3
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at
org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) 在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) 在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) 在org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) 在org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279) 在org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83) at pack1.CheckEveryDropDownLink.main(CheckEveryDropDownLink.java:64)
答案 0 :(得分:1)
这将有效:
我删除了driver.navigate()。back()并将最后一个循环中的下拉列表上的单击移动到循环的末尾,否则在循环开始时关闭下拉列表并且链接不可见
public static void main(String[] args)
{
// TODO Auto-generated method stub
String baseURL = "http://demo.guru99.com/test/newtours/";
String underConsTitle = "Under Construction: Mercury Tours";
int size = 0;
String[] xpath = new String[20];
String temp;
String tempxpath;
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(baseURL);
driver.findElement(By.xpath(".//*[@id='navbar-brand- centered']/ul/li[1]/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
tempxpath = ".//*[@id='navbar-brand-centered']/ul/li[1]/a";
for (int i = 1, j = 0; i < 21; i++)
{
temp = ".//*[@id='navbar-brand-centered']/ul/li[1]/ul/li[" + i + "]/a";
if (driver.findElement(By.xpath(temp)).isEnabled())
{
xpath[j] = temp;
System.out.println(xpath[j]);
j++;
}
}
for (int i = 1; i < 21; i++)
{
String xp = xpath[i];
driver.findElement(By.xpath(xp)).click();
if (driver.getTitle().equals(underConsTitle))
{
System.out.println("The page has invalid content");
} else
{
System.out.println("The page has valid content");
}
driver.findElement(By.xpath(".//*[@id='navbar-brand-centered']/ul/li[1]/a")).click();
}}