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.
Related
Recently I have been tasked with Automating MS CRM 365 with Selenium Automation. I have opted for Using Gradle and Java for this, using IntelliJ.
Currently my issue is when I am on a form page, I am unable to access any elements on that page for data entry or drop-down selection.
I have tried using iFrame switching:
driver.switchTo().frame(driver.findElement(By.cssSelector("#contentIFrame0")));
I have tried selecting from ID, from XPATH & from CSS.
The code is a simple driver.findElement; there isn't anything special about it. I seem to be failing at the most basic part.
driver.findElement(By.id("firstname_i")).click();
Expected outcome: To be able to access the element and process a Click, followed by either a selection from drop-down or a text entry.
Actual outcome:
org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #firstname_i
Image to Firefox Inspection of the Element
I was wondering if there is something in the CSS that's stopping me from accessing the element. I noticed that the element for the 'text' box is under a few layers and is not displayed, unless the top layer is accessed. However I cannot event find that element to follow the flow. Hope that makes some sense.
So after a bit of playing about, I noticed that my iframe switch was working however the element I was trying to access was correct just not complete, not sure if this is ever relevant to anyone else but I had to add a method to counter my element issue by select the initial element "name" and then selecting a second element as the input "name_i".
Where as if I tried to select "name_i" initially I get the whole "element not found" as the CSS sets it to invisible unless activated by mouse over/ click on the first element (overlay?).
This seems to be the case across the whole form page.
Never worked with MS 365 before so I have no idea if this is viable or even common.
Thanks.
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();
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();
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);
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.