how to locate ::Before element in Selenium Java - java

unable to click on this specific "X" element

try with following xpath, It may works.
//*[#id='ngdialog1']//*[#class='ngdialog-close']
In java
driver.findElement(By.xpath("//*[#id='ngdialog1']//*[#class='ngdialog-close']").click();

try using following code:
driver.findElement(By.xpath("//table[#id='ngdialog1']//table//following-sibling::div[1]").click();

Related

Selenium test fails with java

I use Selenium with cucumber in Java and when trying to do that :
WebElement MyAccountLink = driver.findElement(By.className("btn-outlined-white_medium_block"));
MyAccountLink.click();
I got this error:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"btn-outlined-white_medium_block"}
How can i solve it ?
Thanks.
use 'XPath' or 'cssSelector' instead of 'className', because it doesn't matters which locator you are using. you need to find the element and do automation on that element.
WebElement MyAccountLink = driver.findElement(By.xpath("right click on element and copy xpath and paste it here"));
MyAccountLink.click();
Hope XPath will work in any case.

Selenium: find part of identifier

The next command does not work
wd.findElement(By.id("post_message_*"));
The goal is to find id's with post_message_3456346, post_message_01548 and so on. How to fix it?
Actulay there is only one id on page which starts with "post_message_".
Try to locate required element by XPath as below:
wd.findElement(By.xpath("//*[starts-with(#id, 'post_message_')]"));
or by CSS selector
wd.findElement(By.cssSelector("*[id^='post_message_']"));
Try using a css Selector
wd.findElement(By.cssSelector("[id^='post_message_']"));

Locating label element using class

I'm trying to locate the label element and fill it with some value but I'm not able to get it. I'm using Java, testNG and Selenium to write the below code.
The code that I used is below
driver.findElement(By.className("ng-pristine ng-empty ng-invalid ng-invalid-required ng-valid-maxlength ng-touched")).sendKeys("12345");
OR
driver.findElement(By.className("item-input-wrapper scan-input-label")).sendKeys("12345");
This is the details of element that I received when I inspect the element.
Actually selenium doesn't support to locate an element using By.className() with compound class name. You should try using By.cssSelector() instead to locate <input> element as below :-
driver.findElement(By.cssSelector("input[placeholder = 'Scan Container Label']")).sendKeys("12345");
Or more specific :-
driver.findElement(By.cssSelector("input[placeholder = 'Scan Container Label'][ng-model *= 'ctrl.currentValue']")).sendKeys("12345");
Edited :- If you want to locate <label> element using their class name try as below :-
driver.findElement(By.cssSelector("label.item-input-wrapper.scan-input-label"));
Or you can locate it also using By.className() with anyone if the single class name as :-
driver.findElement(By.className("item-input-wrapper"));
Actually selenium doesn't support to locate an element using By.className()
You can try syntax of css with classname, css=tagname.classname
i.e.
driver.findElement(By.cssSelector(input.item-input-wrapper)).sendKeys("12345");

Java Selenium. Find element by text for certain xpath

Trying to find an xpath expression to use in:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();
The element says "Invite Users" on the page and i need to be able to click on it
I need to find the element /a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated
I have already tried these:
"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";
And these give NoSuchElement exceptions.
This is the complete XPATH:
/html/body/div[#class='col-xs-10 col-xs-offset-2 main']/fieldset[#class='form-horizontal']/div[#id='roles']/div[#id='entitlements']/div[#class='panel panel-default '][3]/div[#id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[#class='panel-body']/div[#class='panel panel-default'][1]/div[#class='panel-heading']/h4[#class='panel-title']/a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle']
You can solve it with starts-with():
//a[starts-with(#id, "inviteUsers_")]
If this does not work try find a unique parent.
//div[contains(#id, 'service')]//a[contains(#id, 'inviteUsers')]

Using Selenium webdriver Java to locate elements with unclear Id's classes

Hi I need to know how to locate an element that has no ID or name, and is located within classes, without using xPath for Webdriver
How can I locate the link in the image below without using xPath, using webdriver.
I've included an image:
This CSS selector should work, you can use By.CssSelector:
.links > a[ng-click*="Photos"]
You can use css selectors to select this link by attributes for example by ng-click, data-target, and href attributes
div.links > a[ng-click="loadModal('Photos')" data-target="#myModal" href="#"]
References and tutorials:
http://www.w3schools.com/css/css_attribute_selectors.asp
http://www.sitepoint.com/web-foundations/attribute-selector-css-selector/
Solved it this is what I got and it worked
driver.findElement(By.cssSelector(".links > a[ng-click*=Photos]"));

Categories

Resources