Java 无法通过selenium webdriver定位元素
HTML代码Java 无法通过selenium webdriver定位元素,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,HTML代码 <div id="all_transactions_for_account"> <table class="table table-condensed table-hover"> <thead> <tr> <th>Date</th> <th>Description</th> <th>Deposit<
<div id="all_transactions_for_account">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>Date</th>
<th>Description</th>
<th>Deposit</th>
<th>Withdrawal</th>
</tr>
</thead>
<tbody>
<tr>
<td>2012-09-06</td>
<td>ONLINE TRANSFER REF #UKKSDRQG6L</td>
<td>984.3</td>
<td></td>
</tr>
<tr>
<td>2012-09-05</td>
<td>OFFICE SUPPLY</td>
<td></td>
<td>50</td>
</tr>
<tr>
<td>2012-09-01</td>
<td>ONLINE TRANSFER REF #UKKSDRQG6L</td>
<td>1000</td>
<td></td>
</tr>
</tbody>
</table>
public class Sample {
@Test
public void sampleMethod() throws InterruptedException
{
WebDriver driver;
driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://zero.webappsecurity.com/login.html");
driver.findElement(By.id("user_login")).sendKeys("username");
driver.findElement(By.id("user_password")).sendKeys("password");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("Savings")).click();
WebElement e =driver.findElement(By.xpath(".//*[@id='all_transactions_for_account']/table/tbody/tr[2]/td[2]"));
System.out.println(e.getText());
}
}
不仅是那个元素,我在这个页面中找不到任何元素。请参阅屏幕
因此,请帮助我,我花了2天多的时间,但我找不到解决方案。以下是您需要做的一些更改: 要使用Selenium 3.4.0以及最新的gecko驱动程序0.16和Mozila Firefox 53.0,您需要下载gecko驱动程序并保存它。在
System.setProperty
最后,您的代码将如下所示:
@Test
public void sampleMethod()
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://zero.webappsecurity.com/login.html");
driver.findElement(By.id("user_login")).sendKeys("username");
driver.findElement(By.id("user_password")).sendKeys("password");
driver.findElement(By.xpath("//input[@name='submit']")).click();
}
如果这对您有帮助,请告诉我。您可能需要添加您收到的错误消息。下面是错误gorg.openqa.selenium.NoSuchElementException:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id='all\u transactions\u for\u account']]/table/tbody/tr[2]/td[2]}我正在使用Firefox 31.0,对于这个版本,gecko驱动程序是不必要的。我也尝试了最新的FF和gecko驱动程序,但没有运气。所以请帮助我,我在代码中提到了用户名和密码,你能试试吗?@user3091748如果你使用Firefox 31.0,你可以省略
System.setProperty这一行(“webdriver.gecko.driver”、“C:\\Utility\\BrowserDrivers\\geckodriver.exe”);
其余代码应该适用于您?请更新状态。不,它不工作,我将在新机器中重试并更新