首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >selenium webdriver使用firefox进行google搜索/selenium测试

selenium webdriver使用firefox进行google搜索/selenium测试
EN

Stack Overflow用户
提问于 2018-09-10 01:43:23
回答 2查看 301关注 0票数 0

我正在尝试先打开google.com,然后输入"selenium testing“。

我只想在eclipse中使用webdriver的className,但是我得到了以下错误。

代码语言:javascript
运行
复制
Exception in thread "main" 
   org.openqa.selenium.NoSuchElementException: Unable to locate element: 
   {"method":"class name","selector":"Tg7LZd"}
   Command duration or timeout: 37 milliseconds

下面是我的代码:

代码语言:javascript
运行
复制
package coreJava;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training1 {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        driver.findElement(By.className("gLFyf")).sendKeys("selenium testing");     
        driver.findElement(By.className("Tg7LZd")).click();
    }
}

我该如何解决这个问题?

EN

回答 2

Stack Overflow用户

发布于 2018-09-10 03:14:15

此错误消息...

代码语言:javascript
运行
复制
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"Tg7LZd"}

...implies根据您使用的Locator StrategyGeckoDriver无法找到任何元素。

你的主要问题是你使用的classNames是基于JavaScript的,并且是动态生成的,这在它们生成之前我们无法猜测。作为替代方案,您可以使用以下解决方案:

代码语言:javascript
运行
复制
package coreJava;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training1 {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        WebElement myElement = driver.findElement(By.name("q"));  
        myElement.sendKeys("selenium testing");
        myElement.submit();
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-09-10 14:29:12

代码语言:javascript
运行
复制
System.setProperty("webdriver.gecko.driver", "geckodriver");
FirefoxDriver driver = new FirefoxDriver();

driver.get("https://google.com");
Thread.sleep(3);

driver.findElement(By.className("gsfi")).sendKeys("selenium testing");
Thread.sleep(3);

driver.findElement(By.className("sbqs_c")).click();
Thread.sleep(3);

driver.close(); 

这是工作代码

。这将打开google chrome,然后在搜索框中写入"selenium testing“,然后使用类进行搜索。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52247099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档