Can't inspect search button in IntelliJ-selenium Chrome driver - java

I am working on automation testing website www.benjerry.com/.
I can't inspect serch button.
I succesfully enter credential in search field, but can't click on search button.
I try all kind of combinations, but still nothing.
Can someone please help me?
Picture with marked problem
Code line:
driver.findElement(By.cssSelector("div.btn-submit")).click();

As you have already accepted the answer given from #kushal. I'd like to highlight some points that could be useful in your Automation Journey .
You can use this cssSelector : input[id='searchTerm'] ~button>span
your code would like this :
driver.findElement(By.cssSelector("input[id='searchTerm'] ~button>span")).click();
Here are some benefits of using this cssSelector :
cssSelector are more consistent than Xpath.
Xpath Provided by Kushal contains numeric that is [2], which is never gonna be consistent , your test may fail if Dev would put one more span inside Button Tag. So in case if you still want to go with Xpath , then your Xpath should look like this :
//input[#id='searchTerm']/following-sibling::button/span
You can find the difference between cssSelector and Xpath here :
What is the difference between css-selector & Xpath? which is better(according to performance & for cross browser testing)?

Try following line of code:
driver.findElement(By.xpath("(//button/span[contains(.,'Search')])[2]")).click();

Related

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!

Handle a table in Selenium Webdriver

I am trying to automate a Test Case, but in one of the steps I have a table and I can´t handle it good.
Looking at the code with the development tools, I see a very large list where all the elements of the table are stored. In this link you can see an image of a small part of the code.
http://www.m-i-u.de/display-i94067b1tygv.html
In a certain row of the table is the element "Deadlocked" and I have to check whether in the 2 following rows there are 2 "Nein" (In this case both "Nine" are there)
The thing is that I dont have any ID and I don´t know how to locate this 3 words (Deadocked, Nein, Nein) in the code. Does anybody have any idea I could try?
I would really appreciate any help
Thanks a lot
Pablo
Firefox webDriver element locator plugin is very easy tool to locate elements in UI just by right clicking on elements. You will be able to get the set of selenium locators to identify elements mentioned.
https://addons.mozilla.org/en-us/firefox/addon/element-locator-for-webdriv/
Find all the elements using className attribute. An example:
List<WebElement> links = driver.findElements(By.className("c"));
links.get(0); //this will give Deadlocked
links.get(1); //this will give Nein
links.get(2); //this will give Nein
Using XPath:
List<WebElement> links = driver.findElements(By.xpath("//tr[#class=\"e\"]/th"));
links.get(0);
links.get(1);
links.get(2);
Using cssSelector:
List<WebElement> links = driver.findElements(By.cssSelector("tr[#class=e] > th"));
links.get(0);
links.get(1);
links.get(2);

Is it possible to enter a text in a textbox without specific Element ID

Im doing a test that enter text in the textbox but it dont have specific ID so everytime i run the test it will change. im using selenium webdriver in java please help
The following can work. You can google for them and see how they work.
driver.findElement(By.id("id"));
driver.findElement(By.cssSelector("cssSelector"));
driver.findElement(By.name("name"));
driver.findElement(By.linkText("linkText"));
driver.findElement(By.partialLinkText("partialLinkText"));
driver.findElement(By.className("className"));
driver.findElement(By.xpath("xpath"));
I am sure some of them will be useful. Please let me know if you want more info.
The easiness of using them is in the order which i have mentioned.
What means doesn't have a specific ID?If id at least partially remains the same you can use the CSS locator
driver.findElement(By.cssLocator("input[id*=somePartWhichNotChange]"));
//* star means contains
if not, then you can use cssSelector to get to your element like "body table input" or use xpath as a last resort.

Element not recognized on page until a manual click on the page occurs

I am searching for an element on a webpage by using its XPath. The XPath is correct and can find the element ONLY if I manually perform a click anywhere on the page first. If I do not do this before the statement runs it will not find the element.
I have tried many different attempts to allow focus on the page:
selenium.selectWindow(null);
selenium.fireEvent(xpath, "focus");
selenium.click(element on page);
selenium.mouseDown();
selenium.mouseUp();
After performing these functions the statement will still not find the element, but if I manually maximize the page and click anywhere on the page, then run the statement, the element is found. Can anyone help me out with this?
The statement I am using to find the element is:
selenium.isElementPresent("//div[#class='sample_class']");
Thanks!
Instead of using XPath, try using the CSS selector for that item.
Use selenium.isElementPresent("css=div.sample_class"); as your selector. Saucelabs have explained it quite well at http://saucelabs.com/blog/index.php/2009/10/selenium-tip-of-the-week-start-improving-your-locators/

Categories