相同的CSS选择器在Internet Explorer中不起作用但在Firefox中有效

时间:2013-11-28 19:00:17

标签: java selenium

我在此示例中为元素(链接)(对于gmail页面)创建了一个自定义css选择器。它在Firefox中工作正常,但它在Internet Explorer(版本9.0)中引发错误; IE已启动,但抛出低于错误,我也尝试使用其他定位器,但同样的错误。

Internet Explorer中的错误


线程“main”中的异常org.openqa.selenium.NoSuchElementException:无法找到带有css选择器的元素== a [id ='gmail-sign-in'](警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时: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 个答案:

答案 0 :(得分:0)

第1步: 使用可用的DOM结构,根据您的代码获得完美的对象定位器。已经有对象ID可用于登录链接。

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

执行操作:点击 - 如何

driver.findElement(By.id("的Gmail-登录&#34))。单击();

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