首页 > 解决方案 > Java Selenium“findElement”找不到现有对象

问题描述

昨天我的问题解决后(见下面的链接),新的问题又出现了。首先,上述问题的解决方案仅适用于 IE。当我尝试使用相同的代码来馈送 chrome 或 firefox 时(当然,驱动程序除外),我会收到一开始的错误消息(在我降级 IEDriverServer 之前)。见链接。

Eclipse Java Selenium 无法连接到 localhost

第二。当我使用 findelement 函数时不会发生。错误消息“无法找到元素”。但是,这个元素是存在的。这里应该控制的页面。

https://accounts.google.com/signup/v2/webcreateaccount?continue=https%3A%2F%2Fwww.google.de%2F%3Fgws_rd%3Dssl&hl=de&flowName=GlifWebSignIn&flowEntry=SignUp

这个问题已经经常被要求,但不幸的是答案对我没有帮助。一种解决方案是(因为 findelement 比浏览器更快)安装以下延迟。驱动程序管理()。超时()。隐式等待(5,TimeUnit.SECONDS);

另一种解决方案是搜索 id 或 xpath 而不是 name(这就是 name 和 id 在下面列为 findelement 的原因)。到目前为止没有成功。没有在字段中输入任何提到的键(密码和名字等,我已经尝试过)。

以下是最接近我的问题但对我没有帮助的文章。 没有这样的元素异常 | 互联网浏览器

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

反正。IE 将打开,但不会填充其他浏览器和元素。这里的代码和错误信息。

import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.grid.selenium.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;


public class firefoxöffner {




    public static void main(String[] args) throws IOException, URISyntaxException {
        System.setProperty("webdriver.gecko.driver","./geckodriver");

        String service = "D://IEDriverServer.exe";
        System.setProperty("webdriver.ie.driver", service);
        InternetExplorerDriver  driver = new InternetExplorerDriver();


        driver.get("https://accounts.google.com/signup/v2/webcreateaccount?flowName=GlifWebSignIn&flowEntry=SignUp");

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        WebElement myDynamicElement = driver.findElement(By.id("lastName"));

        driver.findElement(By.id("lastName")).sendKeys("B");
        driver.findElement(By.name("lastName")).sendKeys("A");
    }
}

已启动 InternetExplorerDriver 服务器(64 位) 3.8.0.0 监听端口 47620 仅允许本地连接 Mai 06, 2018 11:29:28 NACHM。org.openqa.selenium.remote.ProtocolHandshake createSession 信息:检测到方言:线程“main”中的 W3C 异常 org.openqa.selenium.NoSuchElementException:无法使用 css 选择器 == #lastName 找到元素

再次感谢。

标签: javaseleniuminternet-explorer

解决方案


如果您使用 Java + Selenium 进行 Web 界面测试,我建议您使用NoraUi开源框架。

该框架管理 IE webdriver、Chrome webdriver 和 FF webdriver。


推荐阅读