Selenium WebDriver Issue with cssSelector - selenium

I am trying to click on a button with using a CSS selector in Selenium 2.0 WebDriver. The issue is the script which I am able to run with Selenium RC is not working with WebDriver.
Code:
Selenium RC:
selenium.click("css=.gwt-Button:contains('Run Query')");
which works absolutely fine.
Selenium WebDriver:
driver.findElement(By.cssSelector(".gwt-Button:contains('Run Query')")).click();
which does not work.
I am using: selenium-server-standalone-2.9.0.jar with Firefox version 5.0. Could anyone help me in figuring out why the cssSelector is not working with WebDriver?

'Contains' is being deprecated from CSS 3. Webdriver supports whatever natively supported by the browser. It works in selenium RC because RC uses Sizzle library for css selectors and it supports 'contains'. Did you try something like,
WebElement element = driver.findElement(By.cssSelector(".gwt-Button[type='button']");
element.click();
If this is not unique then perhaps you might need to filter it further down. If your site uses jQuery then you could use 'contains' selector from jQuery.
JavascriptExecutor js = ((JavascriptExecutor)driver);
WebElement element = (WebElement) js.executeScript("return $(\".gwt-Button:contains('Run Query')\")[0];");
element.click();

that syntax looks valid and when I dropped some elements on a test page and then went to my console and did $(".gwt-Button:contains('Run Query')").length I got a 1 when I changed the text of the buttons. So the only thing I can think of is maybe you have a Run Query button that exists on the page but isn't displayed and so you get the runtime exception. You might want to do a check to see if it's displayed before clicking on it. Or creating a list of the elements and then removing all hidden (display == false) elements.

Related

Selenium WebDriver -- Unable to click on hyperlink, click goes to the other element

I am unable to click on Website hyperlink, the click goes to Recently used pages.
Tried with CSS locator of Website icon [which works in the lower environment as it doesn't have Recently used pages] reference in it.
Tried with XPath Locator[including custom XPath], still, the click goes to another item.
Tried name locator.
Used Actions class for clicking.
Allowed the page to load completely by using sleep and WebDriver wait.
Located the element and send Enter keys, still Recently used pages is clicked.
Tried to click it using coordinates.
Thought of ChromeDriver issue but the issue persists in Firefox too.
Tried below XPath:
html/body/div/div[2]/div[2]/div[1]/a/div
//div[2]/div/a/div
Code snippet:
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
(JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().x+")");
elementToClick.click();
WebElement elementToClick = driver.findElement(By.cssSelector(".icon.siteadmin"));
Actions actions = new Actions(driver);
actions.moveToElement(elementToClick);
actions.click().perform();
Actions builder = new Actions(driver);
builder.moveToElement(elementToClick, 40, 207).click().build().perform();
Result: It clicks on Recently Used Pages, and it yields a result of Recently used pages instead of Website.
UI Reference
Development Code Snippet
Hope It Helps you:
.//div[#id='box_2']/a/div[#class='icon siteadmin']/div[1]
Try the following:
driver.findElement(By.XPath(“//a[contains(#title, ‘Websites’)]”)).click()
If this doesn’t work use the above XPath in combination with one of the moves to element paths above and then use click.

How to resolve org.openqa.selenium.WebDriverException?

I am writing an automated test and want to report bugs, if occur, directly in the repo at GitHub. The step which fails in my program is the Submit new issue button from GitHub Issue Tracker.
Here is the code:
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
And the exception:
org.openqa.selenium.WebDriverException: Element is not clickable at
point (883, 547.7999877929688). Other element would receive the click:
div class="modal-backdrop"></div
The following command also does not work:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", sendIssue);
How can I make it clickable? Is there any other way by which I can resolve this issue?
This is happening because when selenium is trying to click ,the desired element is not clickable.
You have to make sure that the Xpath provided by you is absolutely right.If you are sure about the Xpath then try the following
replace
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
with
WebElement sendIssue =(WebElement)new WebDriverWait(DRIVER,10).until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button")));
sendIssue.click();
If that doesn't work ,You will get an Timeout exception, In that case try incaresing the timeout amount from 10 to 20.
If it still doesn't work please post a screenshot of the HTML.
You need to write something in the issue title and description to make the issue clickable are you sure you are not making that mistake of clicking the button without writing anything in those places I am adding screenshot for your convenience.
Selenium Webdriver introduced in a previous version (v2.48) a new behavior that prevent clicks on elements that may be overlapped for something else (a fixed header or footer - for example) or may not be at your viewport (visible area of the webpage within the browser window).
You can see the debate here.
To solve this you will need to scroll (up or down) to the element you're trying to click.
One approach would be something like this post:
Page scroll up or down in Selenium WebDriver (Selenium 2) using java
Another, and maybe more reasonable, way to create a issue on Github, would be using their API. Maybe it would be good to check out!
Github API - Issues
Gook luck.
This worked for me. Instead of HTML browser this would be useful if we perform intended Web Browser
// Init chromedriver
String chromeDriverPath = "/Path/To/Chromedriver" ;
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);

Selenium sendText

I need to preface this with "I am a noob".
In WatiN I was able to use sendText("text"); which would send the whole text rather than typing it out one character at a time which is what sendKeys() does. I have looked quite a bit for a sendText() option in Selenium and cannot seem to find anything which works.
Is there a sendText() option for selenium, if so could you provide a code example?
In Selenium RC (the older JavaScript-fueled Selenium that is no longer actively developed), there is the type() method.
In WebDriver (also known as Selenium 2), there's no such thing. However, you can easily emulate it via JavaScript:
// only if your driver supports JavaScript
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement elem = driver.findElement(By.whatever("something"));
js.executeScript("arguments[0].value = 'some text'", elem);

Testing for CSS Selenium locators from the browser

In general, when I want to test the validity of a locator to be used in Selenium, I test it using the Firebug console.
i.e., I write: $$("a#someLink") in the Firebug console and the corresponding link becomes highlighted in Firefox.
However, if I test in Firebug for a locator like:
table#someTable tr:nth-of-type(2) td:nth-of-type(2)
Firebug doesn't show anything... Even though the locator works fine from Selenium...
I guess Selenium uses some 'hacks' for CSS locators, which Firebug does not understand...
Is there any way around it? Would using Xpath locators allow me to test for those kinds of locators?
Thank you very much
Firebug has an extension called FireFinder, which allows you to test xpath or css locators and it shows all the matches on the page. Here's a link.

Selenium (WebDriver) cannot see richfaces modal panel

I'm having some issues trying to test elements inside a RichFaces modal panel, as the one
in the demo page of RichFaces here
the issue is that once retrieved an element I cannot interact with it because WebDriver throws a ElementNotVisibleException.
I check it with firebug, and it appears greyed out, because some of the divs have height and width set to 0.
I tried to set all the divs manually with a height and size to see if it changes but there is no way to make it work, so I suppose there must be something else affecting the visibility of the modal panel, but cannot find what.
Has anyone tested webdriver or selenium against a richfaces panel?
Thanks in advance.
Edit:
For the code, is too much to put here, but basically I adapted the jbehave tutorial for the etsy website (the one using spring to inject dependencies), that can be found here.
The architecture is using a PropertyWebDriverProvider that is configured by maven properties to use InternetExplorer or Firefox and is using PageObject pattern (all the pages extend from WebDriverPage).
For specific code, the one from JimEvans gives me the same error.
The following code seems to work for me using the demo site you linked to in your question. It gets the text content of the modal panel, then clicks the "button" to close the panel.
public void testPanel() {
WebDriver driver = new InternetExplorerDriver();
driver.get("http://livedemo.exadel.com/richfaces-demo/richfaces/modalPanel.jsf?c=modalPanel");
WebElement panelShow = driver.findElement(By.id("j_id352:link"));
panelShow.click();
WebElement panel = driver.findElement(By.id("j_id352:panelCDiv"));
WebElement panelTextElement = panel.findElement(By.className("rich-mpnl-body"));
System.out.println(panelTextElement.getText());
WebElement panelCloseButton = panel.findElement(By.id("j_id352:hidelink"));
panelCloseButton.click();
}
Only solution I found out was to do all the interaction with javascript through webdriver