Elad Benda
Elad Benda

Reputation: 36656

selenium get sruck on "findElementBy"

public static void main(String[] args) {
    {


        System.setProperty("webdriver.chrome.driver", "/Users/me/selenium-2.53.1/chromedriver");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
        WebDriver driver = new ChromeDriver(capabilities);

            driver.get("http://www/.google.com");
            WebElement el = driver.findElement(By.id("#myButton"));

            el.click();
            driver.close();

       }
        driver.quit();
    }
}

However my code never returns from driver.findElement(By.id("#myButton"));

not even returning a null element

How can i fix this?

Upvotes: 0

Views: 42

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

#myButton will work with By.cssSelector().. if you want to locate element using By.id(), no need to append # try as below:-

WebElement el = driver.findElement(By.id("myButton"));

Hope it helps...:)

Upvotes: 1

alecxe
alecxe

Reputation: 473803

You would use #myButton with a By.cssSelector() locator:

WebElement el = driver.findElement(By.cssSelector("#myButton"));

In case of By.id() omit the #:

WebElement el = driver.findElement(By.id("myButton"));

Upvotes: 0

Related Questions