Java 使用Selenium选择XPath按钮时出现InvalidSelectorException
我构建了一个类来验证tweet是否提交到Twitter。每当我试图点击submit按钮时,就会收到一个InvalidSelectorException。它似乎有一个非常合理的xpath,采用xpath=//button[@type='button'][17]的形式,所以我不确定它在抱怨什么。有一只雄鸡:Java 使用Selenium选择XPath按钮时出现InvalidSelectorException,java,selenium,selenium-webdriver,webdriver,chrome-web-driver,Java,Selenium,Selenium Webdriver,Webdriver,Chrome Web Driver,我构建了一个类来验证tweet是否提交到Twitter。每当我试图点击submit按钮时,就会收到一个InvalidSelectorException。它似乎有一个非常合理的xpath,采用xpath=//button[@type='button'][17]的形式,所以我不确定它在抱怨什么。有一只雄鸡: public class ValidationTest { private WebDriver driver; private String baseUrl; priva
public class ValidationTest {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
// Google Chrome
File file = new File("C:\\Users\\User\\plugins\\Twitter\\src\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
// Firefox
// driver = new FirefoxDriver();
baseUrl = "https://www.twitter.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testValidation() throws Exception {
// Authentication information
String username = "user123";
String password = "pass123";
// Open site
launchActivity("/");
// Maximize window
maximizeWindow();
// Enter authentication information
sendKeysAndWait("signin-email", username);
sendKeysAndWait("signin-password", password);
// Log in
clickXPathButtonAndWait("//button[@type='submit']");
// Parses a text file to produce an ArrayList from which short (140
// char) messages may be sent
parseStory();
}
/**
*
* @param extension
* The remainder of the URL, after baseUrl
*/
private void launchActivity(String extension) {
driver.get(baseUrl + extension);
}
private void maximizeWindow() throws InterruptedException {
driver.manage().window().maximize();
sleepShort();
}
/**
* s Writes a string to an HTML element and waits a random time
*
* @param elem
* The element to send the keys
* @param keys
* The string to write to the element
* @throws InterruptedException
*/
private void sendKeysAndWait(String elem, String keys) throws InterruptedException {
driver.findElement(By.id(elem)).click();
driver.findElement(By.id(elem)).sendKeys(keys);
sleepShort();
}
/**
*
* @param id
* The id of the button found in HTML
* @throws InterruptedException
*/
private void clickButtonAndWait(String id) throws InterruptedException {
driver.findElement(By.id(id)).click();
sleepShort();
}
@SuppressWarnings("unused")
private void clickCSSButtonAndWait(String id) throws InterruptedException {
driver.findElement(By.cssSelector(id)).click();
sleepShort();
}
private void clickXPathButtonAndWait(String id) throws InterruptedException {
driver.findElement(By.xpath(id)).click();
sleepShort();
}
private void parseStory() throws FileNotFoundException, IOException, InterruptedException {
FileInputStream fstream = new FileInputStream("C:\\Users\\User\\workspace\\Twitter\\src\\text.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
List<String> list = new ArrayList<String>();
// Read the text file line by line
while ((strLine = br.readLine()) != null) {
// Create ArrayList to store text in 140 character chunks
while ((strLine = br.readLine()) != null) {
if (!strLine.isEmpty()) {
list.add(strLine);
String[] stringArr = strLine.split("(?<=\\G.{140})");
System.out.println(Arrays.toString(stringArr));
// Send 1.5 messages per minute (40 messages per hour) for a
// total of 960 per day
for (String s : stringArr) {
clickButtonAndWait("tweet-box-home-timeline");
sendKeysAndWait("tweet-box-home-timeline", s);
//TODO The result is not a node set. and therefore cannot be converted to the desired type.
clickXPathButtonAndWait("xpath=(//button[@type='button'])[17]");
sleepLong();
}
System.out.println("All messages have been sent.");
}
}
}
// Close the input stream
br.close();
}
private void sleepLong() throws InterruptedException {
int randomWaitDuration = 75000 + (int) (Math.random() * 105000);
Thread.sleep(randomWaitDuration);
}
private void sleepShort() throws InterruptedException {
int randomWaitDuration = 500 + (int) (Math.random() * 1000);
Thread.sleep(randomWaitDuration);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
发生此异常是由于xpath表达式无效
"xpath=(//button[@type='button'])[17]"
作为参数传递给ClickXPathButton和Wait方法
试试这个
clickXPathButtonAndWait("//button[@type='button']")
正如您第一次登录时使用的那样。//button[@type='button']是一个非常合理的xpath,但不是xpath=//button[@type='button'][17]您尝试了什么,结果如何?强烈反对张贴所有代码并说“看一看”。请阅读有关如何提问的帮助主题。您需要研究自己的问题,找到代码示例等,并编写自己的代码来解决问题。如果你做了所有这些,但仍然无法理解,那么回来编辑你的问题,并添加你所做研究的注释,你尝试过的代码缩减为a,以及结果是什么。。。任何错误消息等。包括任何相关的HTML并正确格式化HTML和代码也是非常重要的。谢谢。有时候,需要一双新的眼睛才能看到显而易见的东西。
clickXPathButtonAndWait("//button[@type='button']")