使用Selenium切换浏览器时使用Linktext

huangapple go评论64阅读模式
英文:

Switching between browsers using Linktext in Selenium

问题

这是如何在Selenium中使用linkText切换浏览器页面的方式:

使用Selenium切换浏览器时使用Linktext

英文:

This is how you can switch browsers pages using linkText in Selenium as below

使用Selenium切换浏览器时使用Linktext

答案1

得分: 1

public class locatorsPractice {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.gecko.driver","/path");
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.navigate().to("https://browser url");

        String strMainWindowHandle = driver.getWindowHandle();
        System.out.println("Window title" + driver.getTitle());

        driver.findElement(By.linkText("Google")).click();
        Set<String> strHandles = driver.getWindowHandles();
        for (String handle : strHandles) {
            driver.switchTo().window(handle);
            String strTitle = driver.getTitle();
            if (strTitle.equalsIgnoreCase("Google")) {
                System.out.println(driver.getTitle());
                driver.manage().window().maximize();
                Thread.sleep(2000);
                driver.close();
            }
        }
    }
}
英文:
  public class locatorsPractice {

  public static void main(String[] args) throws InterruptedException {
	
	System.setProperty(&quot;webdriver.gecko.driver&quot;,&quot;/path&quot;);
	WebDriver driver =new FirefoxDriver();
	driver.manage().window().maximize();
	driver.navigate().to(&quot;https://browser url&quot;);

	String strMainWindowHandle=driver.getWindowHandle();
	System.out.println(&quot;Window title&quot;+driver.getTitle());
	
	driver.findElement(By.linkText(&quot;Google&quot;)).click();
	Set &lt;String&gt; strHandles=driver.getWindowHandles();
	for (String handle:strHandles) {
		driver.switchTo().window(handle);
		String strTitle=driver.getTitle();
		if(strTitle.equalsIgnoreCase(&quot;Google&quot;)) {
			System.out.println(driver.getTitle());
			driver.manage().window().maximize();
			Thread.sleep(2000);
			driver.close();
		}
	}
	}}

huangapple
  • 本文由 发表于 2020年8月8日 15:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63312962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定