Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Selenium和Java在Google搜索结果页面上单击文本为Gmail的元素_Java_Selenium_Selenium Webdriver_Xpath_Webdriver - Fatal编程技术网

如何使用Selenium和Java在Google搜索结果页面上单击文本为Gmail的元素

如何使用Selenium和Java在Google搜索结果页面上单击文本为Gmail的元素,java,selenium,selenium-webdriver,xpath,webdriver,Java,Selenium,Selenium Webdriver,Xpath,Webdriver,我正在编写我的第一个SeleniumJava脚本,如下所示 import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.supp

我正在编写我的第一个SeleniumJava脚本,如下所示

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class testClass {
    WebDriver driver;
    

    public void launchBrowser() throws InterruptedException {   
        System.setProperty("webdriver.chrome.driver",
        "C:\\Users\\acer\\Downloads\\selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        Thread.sleep(1000);
        
    }
    
    private void sendData() throws InterruptedException {
        WebElement we1 = driver.findElement(By.name("q"));
        we1.sendKeys("GMAIL");
        we1.sendKeys(Keys.ENTER);
        Thread.sleep(2000); 
        we1.findElement(By.xpath("//a[text()='Gmail by Google']")).click();
        Thread.sleep(2000); 

    }


    public static void main(String[] args) throws InterruptedException {
        testClass obj=new testClass();
        obj.launchBrowser();
        obj.sendData();
        

    }

}
它没有错误。但是

we1.findElement(By.xpath("//a[text()='Gmail by Google']")).click();

它不起作用。它不会通过谷歌点击Gmail。还有其他方法吗?

谷歌的Gmail文本在祖先
的子
标签中

因此,不是:

we1.findElement(By.xpath("//a[text()='Gmail by Google']")).click();
您需要使用:

we1.findElement(By.xpath("//h3[text()='Gmail by Google']")).click();

下面的代码对我有用

WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("https://www.google.com/");
    driver.findElement(By.name("q")).sendKeys("GMAIL");
    driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
    driver.findElement(By.partialLinkText("Gmail by Google")).click();

可以使用链接文本的精确或部分匹配来访问链接

所以当你想点击任何元素时,首先要检查链接是否存在。如果有链接,那么最好使用LinkText或partialLinkText

在这里我看到了同样的事情

we1.findElement(By.LinkText("https://www.google.com/gmail/")).click();

@这是因为
Thread.sleep(2000)的时间框架sendKeys(key.ENTER)
和以下
we1.findElement()
之间的时间跨度来说,code>太短了。您需要增加时间范围。
元素信息:{Using=xpath,value=//h3[text()='Gmail by Google']}在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)在sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源)在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知源)在java.lang.reflect.Constructor.newInstance(未知源代码)
中,这并没有提供问题的答案。一旦你有足够的钱,你将能够;相反@尼科哈斯希望现在大家都能理解谢谢。