Selenium Web Driver: findElement(By.name … and headless browser

前端 未结 4 471
借酒劲吻你
借酒劲吻你 2021-01-21 10:25

I\'m trying to follow the Selenium Webdrive Tutorial

http://www.toolsqa.com/selenium-webdriver/headless-browser-testing-selenium-webdriver/

There is a simple te

相关标签:
4条回答
  • 2021-01-21 10:49

    It is working fine at my end and printing the title of page as 'Google'. Though it gave me error at 'find the search button' code.

    Unable to locate element with name: gbqfba
    

    The error seems to be somewhere with your URL as what I can guess is that the driver is not taking the URL into address bar and, hence, not navigating to www.google.com webpage. That's the reason the driver is unable to print the page title and find the search edit box with name 'q'.

    This generally happens due to compatibility issue related to browsers and selenium jar file. Updating the jar files or downgrading the browser may solve this issue.

    0 讨论(0)
  • 2021-01-21 10:50

    Use xpath instead of name.

    try to use this code:

      WebElement searchBox = unitDriver.findElement(By.xpath("//input[@name='q']"));
    

    For search button click:

        // find the search button
        WebElement button = unitDriver.findElement(By.xpath("//input[@value='Google Search']"));
    
        // Click the button
        button.click();
    
    0 讨论(0)
  • 2021-01-21 10:53

    you can try using using xpath with //*[@id='sb_ifc0']

    0 讨论(0)
  • 2021-01-21 11:06

    I've solved .... I'm behind a proxy in my organization so I've to set Proxy.

    I've found this: HtmlUnitDriver does not appear to be loading page.

    Look for FunThomas424242 comment and watch this link https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/htmlunit/HtmlUnitDriver.html

    So the right code is the follow:

    package headlessBrowser;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    
    public class TestOne {
    
    public static void main(String[] args) {
    
        // Declaring and initialising the HtmlUnitWebDriver
        HtmlUnitDriver unitDriver = new HtmlUnitDriver();
    
        // Necessary set Proxy if you're behind it !!!! 
        unitDriver.setProxy("proxy.YOUR-ORGANIZATION.COM", XXXX);
    
        // open google.com webpage
        unitDriver.get("http://www.google.com");
    
        System.out.println("Title of the page is -> " + unitDriver.getTitle());
    
        // find the search edit box on the google page
        WebElement searchBox = unitDriver.findElement(By.name("q"));
    
        // type in Selenium
        searchBox.sendKeys("Selenium");
    
        // find the search button
        WebElement button = unitDriver.findElement(By.name("btnG"));
    
        // Click the button
        button.click();
    
        System.out.println("Title of the page is -> " + unitDriver.getTitle());
    
       }
    }
    

    The "core" rows are the following

        // Necessary set Proxy if you're behind it !!!! 
        unitDriver.setProxy("proxy.YOUR-ORGANIZATION.COM", XXXX);
    

    where you've to update with your proxy configuration.

    0 讨论(0)
提交回复
热议问题