WebDriver can't find element by xpath using Java

后端 未结 6 1812
甜味超标
甜味超标 2021-01-21 22:52

The following is the snippet of WebDriver code using Java:

        WebDriver driver = new FirefoxDriver();
        driver.get(\"http://www.google.pl/\");
                


        
相关标签:
6条回答
  • 2021-01-21 23:29

    In this case the XPath expression you want is:

    //html/body/center/form/table/tbody/tr/td[2]/div/input
    

    Or you could use this (a little more intuitive):

    //input[@title='Google Search']
    

    Keep in mind that if you will be identifying a lot of elements by XPath it would be advisable to become fluent in XPath, you could start here: Xpath Tutorial

    In the meantime, use Firefox and install the following plugins:

    Firebug

    FirePath or Firefinder

    These will help you easily identify valid XPath expressions to use for your website.

    0 讨论(0)
  • 2021-01-21 23:32

    Your xpath expression:

    WebElement query = driver.findElement(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input"));

    looks correct but if you still are facing the issue please check the correctness of xpath again. If it fails again increase the time for Wait as:

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    

    or you can use explicit wait for the specific element as below:

    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input")));
    
    0 讨论(0)
  • 2021-01-21 23:33

    @user729076: The xpath "//html/body/div[2]/span/center/form/table/tbody/tr/td[2]/div/div/input" you wrote for google text field is not right. The HTML for google text field is as follows:

    <input type="text" value="" autocomplete="off" name="q" class="gbqfif" id="gbqfq" style="border: medium none; padding: 0pt; margin: 0pt; height: auto; width: 100%; background: url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false">
    

    Based on the above HTML, you can use simply id or xpath as below: By id:

    driver.findElement(By.id("gbqfq")).sendKeys("some text");
    

    By xpath:

    driver.findElement(By.xpath("//input[@id='gbqfq']")).sendKeys("some text");
    
    0 讨论(0)
  • 2021-01-21 23:41

    If you want to find elements by XPath. Then do the following:

    WebDriver driver = new FirefoxDriver();
    String baseUrl = "http://www.google.com";
    Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
    selenium.open("http://www.google.com");
    selenium.isElementPresent(XPath Variable);
    

    Also find more help on this site

    0 讨论(0)
  • 2021-01-21 23:44

    Since you want the Polish Google site the

        //input[@title='Google Search']
    

    will not work for you. Instead use

        //input[@title='Szukaj w Google']
    
    0 讨论(0)
  • 2021-01-21 23:45

    The XPath Used is Incorrect Here Directly Id is there so no need to use XPath.

    driver.findElement(By.id("gbqfq")).sendKeys("xyz");
    
    0 讨论(0)
提交回复
热议问题