小编Lov*_*ava的帖子

如何在Selenium中获取WebElement的HTML代码

我是测试的新手,所以如果我的问题听起来有些重要,我会提前道歉.

我正在使用Selenium和Java来编写测试.

我知道这 webElement.getAttribute("innerHTML");带来了innerHTML,例如下面的元素:

<a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button" style="position: absolute; border-radius: 0px 0px 4px 4px;">
    <span class="ui-icon ui-icon-closethick">close</span>
</a>
Run Code Online (Sandbox Code Playgroud)

它返回:

<span class="ui-icon ui-icon-closethick">close</span>
Run Code Online (Sandbox Code Playgroud)

但我需要一些东西给我带来WebElement"a"的内在属性,如下所示:

href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button" style="position: absolute; border-radius: 0px 0px 4px 4px;"
Run Code Online (Sandbox Code Playgroud)

java testing selenium selenium-chromedriver selenium-webdriver

23
推荐指数
3
解决办法
4万
查看次数

"ExpectedConditions.visibilityOfElementLocated"和"ExpectedConditions.presenceOfElementLocated"之间的确切区别是什么?

如果我的问题听起来很重要,我会提前道歉,我在QA和Selenium都很新.

两者之间的确切区别是什么:

 wait.until(ExpectedConditions.visibilityOfElementLocated
                    (By.xpath("//a[text()='Show advanced settings...']"))).click();
Run Code Online (Sandbox Code Playgroud)

 wait.until(ExpectedConditions.presenceOfElementLocated
                    (By.xpath("//a[text()='Show advanced settings...']"))).click();
Run Code Online (Sandbox Code Playgroud)

我看过这里,但没弄明白.

java selenium selenium-chromedriver selenium-webdriver

10
推荐指数
1
解决办法
1万
查看次数

如何通过除"class"和"name"之外的属性直接查找WebElements(例如"title")

我是Java和Selenium的新手,所以如果我的问题听起来有些重要,我会提前道歉.

我用:

driverChrome.findElements(By.className("blabla"));
Run Code Online (Sandbox Code Playgroud)

查找具有"blabla"作为其className的元素,例如:

<span class="blabla" title="the title">...</span>
Run Code Online (Sandbox Code Playgroud)

现在,如果我想通过其他属性查找所有元素,该怎么办?就像是:

driverChrome.findElements(By.titleValue("the title"));
Run Code Online (Sandbox Code Playgroud)

这是我目前用来执行此任务的代码:

List<WebElement> spans = driverChrome.findElements(By.tagName("span"));

for (WebElement we : spans) {

    if (we.getAttribute("title") != null) {
            if (we.getAttribute("title").equals("the title")) {
                    ...
                    break;
            }
    }

}
Run Code Online (Sandbox Code Playgroud)

但它并不快速且易于使用.

java testing selenium selenium-chromedriver selenium-webdriver

3
推荐指数
1
解决办法
2万
查看次数

通过Selenium在Chrome中启用弹出窗口

如果我的问题听起来很重要,我会提前道歉,我在QA和Selenium都很新.

我使用Java和硒编写测试,在我的测试的步骤之一,当我点击一个按钮就应该打开另一个窗口,但Chrome会阻止弹出窗口,可我通过硒能弹出?

java selenium selenium-chromedriver selenium-webdriver

3
推荐指数
1
解决办法
1万
查看次数

等待web元素的好方法

如果我的问题听起来很重要,我会提前道歉,我在QA和Selenium都很新.

我使用Java和Selenium编写测试,有时我需要等待Web元素可访问,下面是我以前使用的代码片段:

        int counter = 0;
        while (true) {
            counter++;
             boolean breakIt = false;
            try {
                WebElement element= driverChrome.findElement(By.xpath("bla bla"));
                element.click();
                breakIt = true;
            } catch (Exception e) {             
                Thread.sleep(1000);
            }
            if (breakIt) {
               break;
            }
            if (counter > 4) {
               System.out.println("Failed");
               tearDown();
               System.exit(1);
             }
        }
Run Code Online (Sandbox Code Playgroud)

但现在在某处我看到了这个:

WebDriverWait wait = new WebDriverWait(driverChrome, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("bla bla"))).click();
Run Code Online (Sandbox Code Playgroud)

肯定第二个更短但我不知道它是否更好,换句话说,它们是不同的吗?如果是,怎么样?哪一个更好用于哪些目的?

java selenium selenium-chromedriver selenium-webdriver

1
推荐指数
1
解决办法
561
查看次数