java - 我想验证网站的菜单栏选项卡而不是单击它们

标签 java selenium automation

我想验证站点中的菜单栏选项卡,以确认选项卡名称与其应有的名称完全相同并打印它们。 我尝试使用 linkText() 来做到这一点,但我不知道如何验证它们。

public class Automate2 {

  public static void main(String[] args) {

  System.setProperty("webdriver.gecko.driver","geckodriver.exe");
  WebDriver driver = new FirefoxDriver();
  driver.get("https://www.toolsqa.com/");
  driver.manage().window().maximize();

  driver.findElement(By.linkText("Home"));
  }
}

最佳答案

请尝试代码中提到的任何断言。但是我的代码在 chrome 上运行而不是 Firefox,但断言方法是相同的。

package SeleniumPractice;

import java.io.File;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestSOF {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File filepath=new File(System.getProperty("user.dir") + "\\Executables\\chromedriver.exe" );
        System.setProperty("webdriver.chrome.driver", filepath.getAbsolutePath());
        WebDriver driver=new ChromeDriver();
        driver.get("https://www.toolsqa.com/");
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 10);     
        Assert.assertEquals("HOME",wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".menu-text"))).getText());
        Assert.assertTrue(wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".menu-text"))).isDisplayed());

        System.out.println("Pass");


    }

}

请告诉我代码是否有效。

关于java - 我想验证网站的菜单栏选项卡而不是单击它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54381263/

相关文章:

并发

java - 将数组复制到列表

c# - 64 位和 32 位应用程序之间的自动化/WinAPI 调用

Perl Mechanize follow_link 失败

python - 删除各个子目录中相同的文件名

java - FragmentTransaction 替换不起作用

java - id更新组件的规则

java - Selenium 网格,线程 "main"java.lang.NoSuchMethodError 中出现异常

java - Selenium 用户名密码错误

ruby - 第 1 行中的非法引用。 (CSV::MalformedCSVError)