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
Related
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.
I am trying to click on News link on google search page the HTML structure looks like this
I tried following xpaths but none worked
//a/child::span[1][contains(.,'News')]
The following xpath resulted in invalid selector: The result of the xpath expression "//a/child::span/following-sibling::text()[contains(.,'News')]" is: [object Text]. It should be an element.
//a/child::span/following-sibling::text()[contains(.,'News')]
Thanks
//a[contains(.,'News')] might return this link, but may result in a list of more than one element that you'd need to handle and select the right element from.
You can use Selenium's SearchContext to specify a container element, or solve it using an xpath one-liner like: //div[#role='navigation']//a[contains(.,'News')] (Effectively searching for a link that contains 'News' somewhere in it's html-tree, somewhere inside a div that has a role attribute with value 'navigation').
You simply need
//a[contains(., "News")]
Note that "News" is not a part of span, but a, so your 1st XPath won't work
I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.
I am trying to perform the mouse hover, where 2 elements have same class name. I want to click on second element but its clicking on first element. How to locate the second one if there is only class and href attributes given.
I've tried to create my own xpath using classname and href but its not working.
You can try using the className selector and then using index to get the "2nd" element with that class name.
For this element:
<a class="desktop-main" data-reactid="137" style="border-bottom-color:#fb56c1;" data-type="navElements" data-color="#fb56c1" data-group="women" data-index="1" href="/shop/women">Women</a>
Try this:
List<WebElement> elements = driver.findElements(By.className("desktop-main");
elements.get(1).click();
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");