繁体   English   中英

同一CSS Slector在Internet Explorer中无法使用,但在firefox中可以使用

[英]Same CSS Slector Not Working in Internet Explorer but works in firefox

我在此示例中为元素(链接)(用于gmail页面)创建了一个自定义css选择器。 它在Firefox中运行良好,但是在Internet Explorer(9.0版)中引发了错误; IE已启动,但抛出以下错误,我也尝试了其他定位器,但存在相同的错误。

Internet Explorer中的错误


线程“主”中的异常org.openqa.selenium.NoSuchElementException:无法使用CSS选择器== a [id ='gmail-登录']查找元素(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:307毫秒

Firefox代码-运行正常


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Gmail_FFX {
public static void main(String[] args)
   {
      //Creating Driver and Launching the site
          WebDriver driver = new FirefoxDriver();
      driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
      driver.get("http://gmail.com");
      System.out.println(driver.getTitle());


     WebElement objLink;
     objLink = driver.findElement(By.cssSelector("a[id='gmail-sign-in']"));
             objLink.click(); 
   }
}

Internet Explorer中的代码-引发错误


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

公共类IE_Gmail {

public static void main(String[] args)
   {
      //Creating Driver and Launching the site
      System.setProperty("webdriver.ie.driver", "D:/Selenium/IE Driver/IEDriverServer.exe"); 
      WebDriver driver = new InternetExplorerDriver();
      driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
      driver.get("http://gmail.com");
      System.out.println(driver.getTitle());


      WebElement objLink;
      objLink = driver.findElement(By.cssSelector("a[id='gmail-sign-in']"));
      objLink.click();
 }  

}

第1步:使用可用的DOM结构,按照您的代码获得理想的对象定位器。已经可以在登录链接中使用对象ID。

Gmail登录链接的对象ID:gmail-sign-in

要执行的操作:单击-如何

driver.findElement(By.id(“ gmail-sign-in”))。click();

定位器对象ID更快,并且在所有浏览器中均可访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM