简体   繁体   English

无法通过Selenium WebDriver查找元素

[英]Unable to locate elements through selenium webdriver

HTML Code HTML代码

<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>

Java code Java代码

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());
    }

}

Not only that single element,I not able to find any element in this page. 不仅是单个元素,我也无法在此页面中找到任何元素。 Refer to screen 参照画面

在此处输入图片说明

So please help me I spent more than 2 day but i can't find the solution. 因此,请帮助我,我花了超过2天的时间,但找不到解决方案。

Here are some changes what you need to do: 这是您需要做的一些更改:

To work with Selenium 3.4.0 along with latest gecko driver 0.16 & Mozila Firefox 53.0, you need to download the gecko driver and save it. 要与Selenium 3.4.0以及最新的gecko驱动程序0.16和Mozila Firefox 53.0一起使用,您需要下载gecko驱动程序并保存。 Mention the absolute location of gecko driver in System.setProperty System.setProperty提及壁虎驱动程序的绝对位置

Finally your code will look like: 最后,您的代码将如下所示:

@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();        
}

Let me know if this helps you. 让我知道这是否对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM