Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 如何使用Webdriver检查导航栏?_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

Selenium 如何使用Webdriver检查导航栏?

Selenium 如何使用Webdriver检查导航栏?,selenium,selenium-webdriver,webdriver,Selenium,Selenium Webdriver,Webdriver,我试图检查下图中的两件事: 1.页面上显示的导航栏尺寸正确 2.文本“主页”、“关于我们”等正确显示在导航栏上,并且是web链接(表示它们工作正常且未损坏) 为了检查所有链接是否工作正常,我已经对其进行了编码,但我不确定这是否正确- List<WebElement> linkElements = driver.findElements(By.tagName("a")); String[] linkTexts = new String[linkElements.size()];

我试图检查下图中的两件事:

1.页面上显示的导航栏尺寸正确

2.文本“主页”、“关于我们”等正确显示在导航栏上,并且是web链接(表示它们工作正常且未损坏)


为了检查所有链接是否工作正常,我已经对其进行了编码,但我不确定这是否正确-

 List<WebElement> linkElements = driver.findElements(By.tagName("a"));

String[] linkTexts = new String[linkElements.size()];

int i = 0;

// extract the link texts of each link element

for (WebElement e : linkElements) {

    logger.info(linkTexts[i] = e.getText());

    i++;

}
for (String l : linkTexts) {

    driver.findElement(By.linkText(l)).click();

    if (driver.getTitle().equals(title)) {

        System.out.println("\"" + l + "\""

        + " is not Working.");

    } else {

        System.out.println("\"" + l + "\""

        + " is working.");

    }

    driver.navigate().back();

}
}
List linkElements=driver.findElements(按.tagName(“a”));
String[]LinkText=新字符串[linkElements.size()];
int i=0;
//提取每个链接元素的链接文本
for(WebElement e:linkElements){
logger.info(linktext[i]=e.getText());
i++;
}
用于(字符串l:链接文本){
driver.findElement(By.linkText(l)).click();
if(driver.getTitle().equals(title)){
System.out.println(“\”+l+“\”)
+“不起作用。”);
}否则{
System.out.println(“\”+l+“\”)
+“正在工作。”);
}
driver.navigate().back();
}
}
1)您可以使用“getCssValue”方法获取菜单栏的尺寸

Eg:
    driver.findElements(By.xpath("//td[@class='panelGridInputCol']")).getCssValue("CSSAtribute");
(二) (a) 要检查“主页”和“关于我们”是否正确显示在导航栏上,同样可以使用“getCssValue”方法。 (b)

List allLinks=driver.findElements(按.tagName(“a”));
for(WebElement w:allLinks)
{
w、 单击();
if(driver.findelelement(By.xpath(“页面上的元素”)).isDisplayed()
{
System.out.println(“链接:+w.getText()+”正在工作”);
}
其他的
{
System.out.println(“链接:+w.getText()+”不工作”);
}
driver.navigate().back();//返回主屏幕
}

“但我不确定这是否正确”--这有效吗?遗憾的是,没有。我还没有尝试过Hemasndar提供的答案。谢谢Hemasndar。一个问题-对于第一个问题,我将能够获得您提到的CSS属性,但是我将如何检查它是否正确显示。使用IsDisplayed()也许?@geeko_zac我不明白“正确”是什么意思?你问的是“尺寸”。所以你检查“高度”、“填充”和“边距”。。。。我尝试了你提到的程序,但是我无法检查链接。首先,列出allLinks=driver.findElements(按.tagName(“a”))行给了我一个错误,所以我必须像这样键入cast
List allLinks=(List)driver.findElement(By.tagName(“a”)线程“main”java.lang.ClassCastException中的异常:org.openqa.selenium.remote.RemoteWebElement无法转换为java.util.List
请帮助。它应该是“findElements”,而不是“findElement”是的,我在发布此消息后仅2分钟就意识到了这一点:D该程序检查页面中存在的所有链接。是否有一种方法可以将其限制为仅检查导航栏中存在的链接。
List<WebElement> allLinks = driver.findElements(By.tagName("a"));

    for (WebElement w : allLinks)
    {

        w.click();
        if (driver.findElement(By.xpath("Element on the page")).isDisplayed())
        {
            System.out.println("Link:"+w.getText()+"is working");
        }
        else
        {
            System.out.println("Link:"+w.getText()+"is not working");
        }
        driver.navigate().back();//To come back to the Home screen

    }
const selector = { className: 'navbar-nav'}

const navbar = () => driver.findElement(selector);

expect(await navbar().getText()).toContain('Home');
expect(await navbar().getText()).toContain('About Us');