Locating label element using class - java

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");

Related

cant find the element using classname in selenium java

I'am using selenium framework to test my own website. Im trying to click on specific icon which using anchor tag. I have java selenium code to click but couldn't click. Tried many xpath, css selectors, class name and names. but didn't worked. But can run the script and it is opening the chrome and navigating to entered domain but the clicking option is not working
above code I need to click nav-twitter class anchor option . which will create another tab in chrome to show my twitter page. but after running the app .it is only navigating to the page domain and nothing works.
So, This is my code where I have added. until the maximize everything works but not the anchor tag
This kind of error im getting when running the script in chrome. Please anyone let me know where I have been wrong here or is there are any way to make it happen. Thanks in advance.
Whenever you are initiating webdriver make sure to add implicit wait statement, so it can wait for sometime before looking for objects. Add below statement right after chromedriver initialization and your code should work without any issues.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Your locator is slightly off. In your code you are looking for an element with tagName as twitter which doesn't exists. Instead you can use either of the following locator strategies:
Using cssSelector:
WebElement navTwitter = driver.findElement(By.cssSelector("a.nav-twitter[name='twitter']"));
navTwitter.click();
Using xpath:
WebElement navTwitter = driver.findElement(By.xpath("//a[#class='nav-twitter' and name='twitter']"));
navTwitter.click();
You have used wrong selector/locator here. "twitter" is not tag name, Tag name is the value from less than symbol to the first space or greater than symbol. Words like div, p, a, span are the tag names.
So here a is the tag name and name, class, href are attribute which you can use while forming xpath or css locators. So you can create multiple locators here:
By using class name attribute
By.className("nav-twitter");
By creating xpath using name attribute
By.xpath("//a[#name='twitter']");]
By creating xpath using class attribute
By.xpath("//a[#class='nav-twitter']");
By creating css locator using class attribute.
By.cssSelector("a[class*='nav-twitter']");
By using dot symbol in css selector with class name
By.cssSelector("a.nav-twitter");
Similarly you can create css selector with name too. Since there are multiple tags, so you can not use a tag directly and If you want to use By.tagName("a") then it will click on first tag present on the page
Note: We use dot with class name and Hash (#) with Id name while forming css selectors.

Can not find button. Selenium java

I have this code:
Then using selenium I write this:
driver.findElement(By.className("extra-delivery-item")).click();
The question is why does selenium tell me:
no such element: Unable to locate element: {"method":"css selector","selector":".extra-delivery-item"
P.S. I have already located some buttons and successfully clicked on them, everything works, except that button.
This element is actually has multiple class names. So to select it according to single class name you can use XPath or CSS selector.
Like this:
driver.findElement(By.xpath("//div[contains(#class,'extra-delivery-item')]")).click();
or
driver.findElement(By.cssSelector("div.extra-delivery-item")).click();
Selecting element by class name, like you tried to use
findElement(By.className("extra-delivery-item"))
means matching element with class attribute exactly matching that value, extra-delivery-item in this case. While the element class attribute here is extra-delivery-item extra-delivery-item-selected

How do i click this element via className since i will be using it for multiple screens with same class name

The element I got is <div class="x-tool x-box-item" id="tool-123" src="data:image/gif;base64" class="new-img x-tool-maximize".
I particularly need this class="new-img x-tool-maximize" because its the common of all the screen.
I already tried
driver.findElement(By.className("new-img.x-tool-maximize")).click()
and
driver.findElement(By.className("new-img x-tool-maximize")).click();
and
driver.findElement(By.xpath("//div[contains(#class, 'value') and contains(#class, 'test')]"))``;
You should be able to use a CSS selector to find this type of element. You’ll want something like driver.findElement(By.cssSelector("div.new-img.x-tool-maximize")).
You can use list in selenium if you have multiple elements with same locators
List<WebElement> classes=driver.findElements(By.classname("new-img x-tool-maximize));
// if you want click 1 st class name element use this following line
classes.get(0).click();
OR // if you want click 2 nd class name element use this following line
classes.get(1).click();
Use a list to get all the WebElements with specific classes
List<WebElement> list = driver.findElements(By.cssSelector("div.new-img.x-tool-maximize"));
As you intent to click() on the element using only the className attribute values, ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector using only the className attribute values:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(".new-img.x-tool-maximize"))).click();
For a more canonicl approach you can indlude the tagName as follows:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.new-img.x-tool-maximize]"))).click();
References
You can find a couple of relevant detailed discussions in:
Unable to locate element using className in Selenium and Java
How to locate the last web element using classname attribute through Selenium and Python
What are properties of find_element_by_class_name in selenium python?

accessing the class name Selenium Webdriver

I am trying to access a web element in this example I am trying to access the className
driver.findElement(By.className("more-option")).click();
from bellow but it fails.
<div class="text-center">
<a class="small text-muted js-more-options" href="#">More
Options</a> = $0
</div>
My goal is to be able to test the ability to click More options button
Edit
I have tried
driver.findElement(By.cssSelector("td[title='More options']")).click();
and
driver.findElement(By.partialLinkText("options")).click();
There are to many option you can click on element.You can use contains().
Contains() is a method used in XPath expression.
driver.findElement(By.XPath("//a[contains(text(),'More Options')]")).Click();
or
driver.findElement(By.XPath("//a[contains(#class,'small')]")).Click();
if you get more than one element then you have to use index and click on particular element.
Find element using By.className just for single class name.
Try following approach.
By css selector:
driver.find_element_by_css_selector('div.text-center a.small.text-muted.js-more-options').click()
driver.find_element_by_css_selector('a[class="small text-muted js-more-options"]').click()
By xpath:
driver.find_element_by_xpath('//div[#class="text-center"]//a[#class="small text-muted js-more-options"]').click()
By partial link text:
driver.find_element_by_partial_link_text('Options').click()

How to deal with Dynamically changing class names in HTML source code while automating using Selenium WebDriver

How to deal with Dynamically changing "class names" or "ids" in HTML source code while automating using Selenium WebDriver
Example Application : GMAIL
if you know the cell index then using XPath or cssSelector will solve the issue. Here in the above screenshot it is clear that the text "Wordpress" is in 5th cell of each table row/so you can use below XPath to get the particular cell.
List<WebElement> cellValue = driver.findElements(By.xpath(".//table/descendant::tr/following-sibling::td[5]"));
The Selenium mechanism for locating elements (org.openqa.selenium.By) offers other methods of locating elements.
Example: name, partialLinkText, css selector, xpath selector, tagname, linkText.
This tutorial from Selenium Easy contains some easy to understand examples.
If you want to locate <span name="WordPress">WordPress</span> element, You can locate this element using By.name() as below :-
driver.findElement(By.name("WordPress"));

Categories

Resources