我正在尝试单击一些按钮,并使用 twitch 上的“关注”按钮作为示例。
我使用 Selenium IDE 尝试获取按钮的 xpath。我得到的 xpath 是: //span[@id='ember637']/a/span
如果我转到 FireFox 并复制按钮的唯一选择器,我会得到: .js-follow > span:nth-child(1)
我已经在java程序中尝试过,但它们不起作用。当我使用 //span[..
xpath 时,出现以下错误:
"Unable to locate a node using //span[@id='ember637']/a/span"
编辑:
带有我要单击的按钮(“关注”按钮)的网站示例: http://www.twitch.tv/mradder89/profile/
我使用的 Selenium jar 文件是“selenium-server-standalone-2.35.0.jar”
我收到的错误是
"Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //span[@id='ember637']/a/span"
编辑2:
我下载了 PhantomJSDriver exe 文件(phantomjs.exe)并正在尝试。这不起作用... 我没有收到像以前一样的错误消息(“无法找到节点...”错误)。
最佳答案
使用 PhantomJSDriver,尝试不同的定位器。如果有任何异常,则发布异常,否则发布元素的信息,例如位置、文字等
driver.findElement(By.xpath("//*[contains(@class, 'profile-actions')]//span[text()='Follow']")).click();
driver.findElement(By.cssSelector(".profile-actions .primary_button > span")).click();
关于java - Selenium WebDriver - Java - 单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19084041/