如何导出 Google 搜索栏的 CSS 表达式或 X 路径?

问题描述 投票:0回答:1

我想为谷歌搜索栏派生cssExpn或Xpath,然后在java selenium程序中使用该元素。 我在控制台上尝试了以下表达式,但长度为 0,这意味着我无法在 selenium 程序中使用这些表达式。


    $x("//input[@type='search']");
    $x("//input[@placeholder='Search Google or type a URL']");
    $$("input[type='search']");


package java_selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Sample1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        WebElement Element=driver.findElement(By.xpath("//input[@placeholder='Search Google or type a URL']"));
        String text=Element.getTagName();
        System.out.println(text);
driver.close();
    }

}
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@placeholder='Search Google or type a URL']"}
  (Session info: chrome=120.0.6099.129)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.16.1', revision: '9b4c83354e'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [7fa318a6dca91ff1153c3aef72ca1202, findElement {using=xpath, value=//input[@placeholder='Search Google or type a URL']}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 120.0.6099.129, chrome: {chromedriverVersion: 120.0.6099.109 (3419140ab66..., userDataDir: C:\Users\SUNIL\AppData\Loca...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:60748}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:60748/devtoo..., se:cdpVersion: 120.0.6099.129, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: 7fa318a6dca91ff1153c3aef72ca1202
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:52)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:191)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:200)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:175)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:523)
    at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165)
    at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:59)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:354)
    at java_selenium.Sample1.main(Sample1.java:14)
java google-chrome selenium-webdriver selenium-chromedriver console
1个回答
0
投票

您尝试访问的元素不是

input
,而是
textarea

因此,选择器应该是

//input[@type='search']
甚至更简单
//textarea[@type='search']
,而不是 
//*[@type='search']

    driver.get("https://www.google.com");
    driver.findElement(By.cssSelector("[type=search]")).sendKeys("test search");
    driver.findElements(By.cssSelector("input[type=submit][role=button]"))
                .stream()
                .filter(WebElement::isDisplayed)
                .findFirst()
                .get()
                .click();
© www.soinside.com 2019 - 2024. All rights reserved.