为什么无法定位元素错误?

时间:2018-07-02 08:49:56

标签: selenium selenium-webdriver

我已将Xpath分为4部分,因为在Xpath中,只有一行的行值更改为/tr[2],直到路径结束为止的行数为3,4,5。

public class NewTest {

    @Test
    public void f() throws InterruptedException 
    {
        System.setProperty("webdriver.chrome.driver", "F:\\New folder\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.get("http://xxxxxxxxxxx/index.aspx");

        driver.manage().window().maximize();

        WebElement ee= driver.findElement(By.id("ddlstore"));
        Select s11 = new Select(ee);
        s11.selectByVisibleText("xxxxxx");

        String s1 = "/html[1]/body[1]/form[1]/div[5]/center[1]";

        String s2 = "/div[1]/table[1]/tbody[1]/tr[";

        String s3 ="]";

        String s4 = "/td[11]/input[1]";

        for(int i=2;i<=99;i++)
        {
            String finalXpath= s1+s2+i+s3+s4;

            driver.findElement(By.xpath(finalXpath)).click();

            Thread.sleep(3000);

            try {
                WebElement e1 = driver.findElement(By
                        .xpath("//p[@id='skipcount']"));
                System.out.println(e1.getText());

                WebElement e2 = driver.findElement(By.xpath("/html/body/div[1]/div/div"));
                WebElement e3 = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[3]/button[2]"));
                Actions a1 = new Actions(driver);
                a1.moveToElement(e2).click(e3).build().perform();

            } catch (Exception e) {
                System.out.println("Can't Click on The Element ");
            }
        }

元素未单击就显示错误,Unable to Locate Element,Xpath

第一个Xpath分为字符串的HTML代码:

<input type="submit" name="CustomPaging_GridView$ctl02$ff" value="SKIP" onclick="product_skip(this);" id="CustomPaging_GridView_ff_0" class="button2" data-id="12691247570" data-id1="36025">

在Try / Catch块中的Xpath的HTML代码:

<div class="modal-footer">
    <span id="prcid" style="display:none;">processing...</span>
    <button type="button" id="skipok" onclick="skipoverall(this)" class="btn btn-primary" data-id="12691247570" data-id1="36025">Ok</button>
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>

Ps : Actually The Doubt is, I Need To Click an Skip Button Which has an Same Xpath For All Button and Each Time When i Click Skip Button an Popup Will Appear in That i Need To Click The Cancel Button

2 个答案:

答案 0 :(得分:0)

尝试一下:

int i = 1;

 String s2 = "/div[1]/table[1]/tbody[1]/tr[' " +i+ " '];

不需要String s3。

答案 1 :(得分:0)

尝试一下:

// you don't need 'e2', instead you hover 'e3' and click on it
WebElement e3 = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[3]/button[2]"));
Actions a1 = new Actions(driver);
a1.moveToElement(e3).click(e3).build().perform();

建议:

    String s1 = "/html[1]/body[1]/form[1]/div[5]/center[1]";

    String s2 = "/div[1]/table[1]/tbody[1]/tr[";

    String s3 ="]";

    String s4 = "/td[11]/input[1]";

    for(int i=2;i<=99;i++)
    {
        String finalXpath= s1+s2+i+s3+s4;

可以替换为:

for(int i=2;i<=99;i++)
    {
        String finalXpath= "/html[1]/body[1]/form[1]/div[5]/center[1]/div[1]/table[1]/tbody[1]/tr[" + i + "]/td[11]/input[1]";
        ...