Selenium Webdriver - Cannot click button - Finds element - java

Hopefully someone can help me with a solution to my problem. I have spent the better part of the day trying nearly every solution I can find on here and through Google.
I will cut to the chase. I have a test that logs in on one screen. The second screen selects a role and moves on. I am able to get to the role selection screen, but cannot move forward as Selenium will not click the button, regardless of what I do.
The interesting thing is that this will work with the IDE, but exporting the code to java and running it with the webdriver does not work either. This part makes no sense to me as I would think the IDE export should run easily. The only difference I can see is that the webdriver pulls up a profile-less Firefox, whereas the IDE is running on my profile - not sure that this would have anything to do with it, but wanted to throw that out there.
The only difference between the first button and the second button is the autofocus command in the HTML.
<button data-submit-button="" class="btn" autofocus="" name="submit" type="submit">Go</button>
I've tried numerous solutions found throughout the web and the last thing I've tried is
new WebDriverWait(driver,120).until(ExpectedConditions.elementToBeSelected(By.name("submit")));
driver.findElement(By.name("submit")).click();
This does not work either. I have tried several variations on this and most of the time the webdriver either times out or skips through this step, passing it and then failing on the next steps as the page has not moved forward.
Does anyone have any suggestions? If you have any questions or I can give you more information if I haven't given enough! Any help with this would be amazing, as I've spent the better part of the day trying to get through this one issue.
Thanks!

Sometimes the selector is the main issue in the scenario like this. Also, name="submit" is not even a very safe selector to use since developers have tendency to duplicate the same attribute across the form. Without knowing about the actual elements I can pretty much tell you the issue is the selector. If the text Go is the only text of the button on that page, give the following xpath a shot
//button[#name='submit'][#type='submit'][contains(text(),'Go')]
Complete implementation should look like as follows:
By xpath = By.xpath("//button[#name='submit'][#type='submit'][contains(text(),'Go')]");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(xpath));
myDynamicElement.click();

Related

Can we use one pageobject among different pages?

I am working on one selenium project. My scenario is, I have multiple forms one after another. After filling them I have to click on the Next button. I have 4 pages with the next button and they have the same XPath. When I try to use the same web element for the next button it works once but when I try to use that same thing again on a different page It shows element intreactable error.
I would try the following to identify why you get the interactable error (it would help to give us the error at least as is found on the Selenium Documentation)
try to click using javascript executor,
try to add webelements for each action you need to perform (so 4 webelements for each 'Next' button). If it works in this way maybe the webelement is not refreshed after you use it (maybe is a static field). Try also to work with Page Factory pattern.
If nothing works, we should receive more information to be able to help.

Can I use the PrimeFaces captcha from their showcase as it is?

I ve tried the latest PrimeFaces Captcha on their showcase:
https://www.primefaces.org/showcase/ui/misc/captcha.xhtml
and read an existing question on stackexchange:
How to use PrimeFaces Captcha?
I am still not sure how (and whether) the captcha on the primcefaces page does work. Whenever I click with my mouse somewhoere on it, it alsways says : "Correct" . So even I always click on one and the same place, it says "Correct". Is that all simple ?!!! Can I use the showcase in my code as it is?
Thank you
Apparently the Captcha starts to work after several cliks on it - I made maybe seven or ten clicks which were all accepted, but afterwards the capctha started to behave as a "real" captcha showing me pictures and asking me questions about the pictures.
Important: before using the primeFaces captcha on their showcase, one must update the web.xml file with two important parameters as shown here:
Primefaces 5: captcha does not work for 'javax.servlet.ServletException: Expression cannot be null'

Java Selenium - How to interact with id-less/class-less element?

I'm trying to interact with a button on a page. Linktext and xpath do not work, there are no classes or combinations of selecting elements and looping through them I can find that work.
Here is the screen shot of the code I'm trying to do a .click()
Please help me how do i achieve the same ?
I think you have 2 options as below. I simplified your example HTML code to smoke test these queries:
Select an element based on its content. The drawback is of course that as soon as "Historical Scans" label changes to something else your query will stop working.
//nav[#id='secondaryNav']//ul[contains(#class, "menu")]//a[normalize-space(.)="Historical Scans"]
(working example on xpath tester http://xpather.com/dqZ7UWvz)
Select an element based on the position on the list. The downside is that it will stop working once this element changes its position.
//nav[#id='secondaryNav']//ul[contains(#class, "menu")]/li[3]/a
(http://xpather.com/rgexHKBB)
Based on my experience you should not rely on any other attributes or elements. Ideally, the best option would be to add ids/classes. Please let us know if this solves your problem.

Selenium WebDriver - Facebook Posting Issue

I'm really having trouble automating the facebook posting process.
My script is simply unable to track the textarea in our news feed correctly.
I've tried different selectors, even the ones suggested by Selenium IDE but got no avail.
Here's a snippet of my code:
//xpath for status update box
WebElement statusUpdateBox = driver.findElement(By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN));
//click the status update box
driver.findElement((By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN))).click();
//input data in the status update box
statusUpdateBox.sendKeys(DemoTestData.DATA_STATUSUPDATE_MSG);
WebElement postButton = driver.findElement(By.xpath(DemoTestData.XPATH_STATUSUPDATE_BTN));
//click 'post' button
postButton.click();
The xPath for
DemoTestData.XPATH_STATUSUPDATE_BTN = "html/body/div[1]/div[2]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div/div[2]/div/div/div/form/div[1]/div/div[2]/textarea";
What am possibly missing?
Thanks a lot. :D
Here is what I've found. When I checked your xpath in my FB page it returned no results. (a very handy tool is a plugin called free simple firepath in firefox, be sure to get it if you don't!)
It seems unlikely that the button has an xpath ending with "textarea" are you sure that is correct?
When you run your test does the text get filled in? If not, the (first) problem is probably the findElement of the textbox.
When I looked some more I found that the css is actually very simple "textarea" . So if you'd use driver.FindElement(By.CssSelector(“textarea”))
that should return the text area.
The button has a bit of a strange css (to me, but I am quite new to this as well), but I beleieve you could find that by the text in it. In my language it says "Plaatsen" It might say something different for you. Check in Firepath!
The something like driver.FindElement(By.XPath("//*[text()[contains(.,'Plaatsen')]]")) should return the button.
I hope it helps! And I hope you found this answer useful, any tips on that would appreciated, it my first time answering something on the mighty Stack Overflow!
Cheers!

Selenium Webdriver: IE can't find radio buttons nor checkboxes for using click() on them

I am building a test suite for a basic registration page (it has a large number of text fields + some radio buttons and checkboxes here and there). I am using Selenium Webdriver solution and tests are written in Java. While the tests are running fine on Firefox and Chrome, the Internet Explorer tends to run into trouble when it comes to clicking on radio buttons or checkboxes. All the radio buttons and checkboxes have id-s defined and from what I've learned it's the most convenient way to find an element on the page, so I was quite surprised when I started getting these issues. The method for finding the radio button looks like this:
public static WebElement rad_Male(WebDriver driver) {
element = driver.findElement(By.id("male"));
return element;
}
The click is done in a following way:
rad_Male(driver).click();
As I said, Firefox and Chrome can easily click on checkboxes and radio buttons, but when running tests in IE I get a following exception (the element is visible all the time and I can click on it with mouse):
org.openqa.selenium.ElementNotVisibleException: Cannot click on element
I've also tried using an explicit wait in order to let the elements load before accessing them, but had no luck - I get TimeoutException as soon as the function times out. I suspect it has something to do with the page design, but unfortunately I have no access to the page source code, so I cannot change the page structure to make it easier to test.
The radio button is placed inside a number other divs and I think there is also a table used to align this and other elements around, but this doesn't look too complicated. Here's the code for radio button:
<input type="radio" value="M" name="sex" id="male" tabindex="110">
I think I saw some javascript click suggestion in one of the similar topics, but before resorting to this I wanted to make sure that there is no other way to make it work using the means that Webdriver provides.
I've just started learning Selenium and I try to get my work done on the go while learning new stuff all the time, so I am not too experienced with this yet.
If you would like some more details, please ask as I am not sure if I've got all included.
Thanks in advance!
Hi again and thanks to everyone who responded! A friend of mine had a look at this issue and managed to figure out what was causing this. The radio button was actually contained inside another div in a following way:
<div class="radio" id="uniform-male">
<span>
<input type="radio" value="M" name="sex" id="male" tabindex="110">
</span>
</div>
It appears that this parent div "uniform-male" kind of concealed this button, because Selenium was able to click on this div and as a result, the radio button underneath it was clicked.
I guess I should have posted the code for the radio button along with some code of it's parent elements in the first hand, so it would have been easier to debug it.
Once again I appreciate all the help I received from you on this question, thanks!
try using this before you click on the element, maybe IE is a bit slower:
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(anId)));
As per the error you are getting, I think selenium is trying to click on the element, i.e., radio button, which is probably not visible yet.
To resolve this, try adding an explicit wait in the method rad_Male like this (Assuming 'element' is a reference of 'WebElement class'):
public static WebElement rad_Male(WebDriver driver) {
//waiting 30 seconds for the element to be visible
element = new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.id("male")));
return element;
}
Then, use it to click the button like this:
element = rad_Male(driver); //Fetching the value returned by rad_Male method
if(element!= null)
element.click();
else
System.out.println("Element is not visible");

Categories