Clicking on a button based on some text in div - java

How can I click on a button based on the text "See More" or "Sign Up"
<div data-reactid=".0.0.$LandingPage.0.1.0.0.1.0.3">
<button class="arda-button btn -primary" data-reactid=".0.0.$LandingPage.0.1.0.0.1.0.3.$main-menu-see-more">See More</button>
<button class="arda-button btn -primary" data-reactid=".0.0.$LandingPage.0.1.0.0.1.0.3.$main-menu-sign-up">Sign Up</button>
</div>
</div>
Or based on the html above can you see a unique way of identifying and clicking on the two buttons?
Thanks

Identify using contains text() as 'see more' and 'sign up', should work.
By.xpath("//button[contains(text(),'See More')]")
By.xpath("//button[contains(text(),'Sign Up')]")

You can use XPATH ( Consider the Xpath is not big and Ugly) where each button will be separate.
If XPATH is ugly and long use regular Expression where data-reactid CONTAINS "main-menu-see-more" for "See More" button. And data-reactid CONTAINS main-menu-sign-up for Sign up button
"//*[contains(#data-reactid,'main-menu-see-more')]"
" //*[contains(#data-reactid,'main-menu-sign-up')]"
What about linked Text "See More" and "Sign Up".
Please share your feedback its working not......

I avoid XPath like the plague unless absolutely necessary because it's complicated, slower than other methods, and more prone to breaking (in general). I would do this using a CSS Selector.
The data-reactid is unique for each of these elements so I would use those to find the desired elements instead of the text.
driver.findElement(By.cssSelector("button[data-reactid='.0.0.$LandingPage.0.1.0.0.1.0.3.$main-menu-see-more']")).click();
This code finds a button that has the specified data-reactid and clicks it.
CSS Selectors are very powerful and it lets you avoid XPath... which is a good thing. :)
CSS Selector reference

Related

Cannot click on a button using SELENIUM and JAVA - triggering windows popup to upload file

I cannot click on a specific button.
Its name is "Select file" and it should open a windows popup:
<a class="btn btn-primary btn-sm btn-upload">Select file</a>
The XPATH is:
//*[#id="div-add-file"]/a
I tried something like this but it seems not to be clicking anything and that's strange:
driver.findElement(By.xpath("//*[#id=\"div-add-file\"]/a")).click();
I also tried something like this:
driver.findElement(By.linkText("Select file")).click();
What's going on here?
First note is that if possible, always tag the element itself with the id so your selectors are trivial.
Also, CSS selectors are usually more readable for humans. Under the hood, it's all xpath in the end, so it's good to be familiar with it, but it's not optimal. It's not really possible to diagnose your issue with the information provided. One possibility is that the selector is wrong, another is that the element is not loaded yet and you need to include a wait. A third possibility is your driver, or the browser version. Selenium has many things that can potentially go wrong
If the class value is unique, then you can use:
driver.findElement(By.xpath("//*[#class='btn btn-primary btn-sm btn-upload']")).click()
(or)
driver.findElement(By.cssSelector(".btn.btn-primary.btn-sm.btn-upload")).click();
otherwise:
driver.findElement(By.xpath(".//a[contains(text(),'Select file')]")).click();

Selenium search text in a page section and click on a button inside it

I am using Webdriver and Java. My problem is that the page is splitted in 2 sections, both with the same name //div[#class='sg-content-box sg-content-box--spaced']
I want to click on the button Aprobă near the user David2211 (his answer can appear first or second)
My problem is that i don't know how to do click on the button in that specific area, for that specific username
For things like this, you have to use XPath since we are looking for an element that contains specific text. In this case, we will be looking for an element that contains the username 'David2211' and then a button that contains the text 'Aprobă'. I would assume that you will use this code more than once using different usernames so I would put this in a function so that it's easier to reuse.
The function
public void approveAnswerByUsername(String username)
{
driver.findElement(By.xpath("//a[contains(.,'" + username
+ "')]/ancestor::div[contains(#class, 'js-answer-element')]//div[contains(#class,'js-approve-button-text')][contains(.,'Aprobă')]")).click();
}
and you call it like
approveAnswerByUsername("David2211");
This XPath is rather large and complicated but it looks for an A tag that contains the specified username, goes up the DOM to find a parent DIV that encloses the entire answer and then navigates back down the DOM to find a DIV that contains the 'Aprobă' text.
Try Below xpath to locate the same element
Here you have to pass the user's name whose button you wants to click
//li/a[contains(.,'David2211')]/../../../preceding-sibling::div//button[#title='Aprobă']
The only thing specific to your button is indeed the username. I would suggest to get the xpath of the username's node, and then try to get its button from the parent's node using the xpath :
//{username's xpath}/parent::div[#class='sg-content-box sg-content-box--spaced']/{button's xpath}
It seems quite barbaric but that's the only way I see.
A challenging xpath after many days. Thanks for asking this question. Try this below xpath.
"//li/a[contains(text(),'DemonBolt')]/ancestor::div[#class='sg-content-box sg-content-box--spaced']//div[#class='sg-label__text js-approve-button-text']"
or
"//li/a[contains(text(),'DemonBolt')]/ancestor::div[#class='‌​sg-content-box sg-content-box--spaced']//div[contains(text(),'Aprobă')]"
Just change the text DemonBolt with the name whom you need to approve.
Hope this helps. Thanks.
Here is the Answer to your Question:
To click on the button Aprobă near the user David2211 (his answer can appear anywhere) you can use the following code block:
JavascriptExecutor je = (JavascriptExecutor)driver;
WebElement element = driver.findElement(By.xpath("//a[contains(text(),'David2211')]//preceding::button[2]/div/div[contains(text(),'Aproba')]"));
je.executeScript("arguments[0].scrollIntoView(true);",element);
element.click();
Let me know if this Answers your Question.

Unable to click on item from 'onclick' partial value even xpath

Is it possible to click an element through selenium by a partial value of an onclick element? I had try using xpath but it seems not working even on partial value.
There are multiple input items on a page, and I only need to onclick on specific string = 锁定. Kindly advise , Thanks you
HTML:
<button class="button_d" onclick="lock('/deposit/ajaxLock.html?oid=12016062862662862','锁定')">锁定</button>
<button style="display:" class="button_d" onclick="depositOk(this , '12016062862662862',53309)">确定</button>
MY CODE :
driver.findElement(By.xpath(".//input[contains(#onclick, '锁定')]")).click();
I'm not an XPath expert but this CSS selector should do what you ask. It's looking for a BUTTON that has an onclick attribute that contains the string, 锁定.
driver.findElement(By.cssSelector("button[onclick*='锁定']")).click();
You could also just look for a BUTTON that contains the desired string. The onclick string and button text seem to be the same, at least in the example HTML you provided.
Some CSS selector references if you want to learn more...
CSS Selector reference
CSS Selector Tips

Selenium Web driver clicking images

How do I select in image on and click on it using Selenium web driver? Say if it says this
<style type="text/css"> <ul id="nav"> <li> <li> <li> <li> <li> My Dashboard </li> </ul>
Would I use
driver.findElement(By.linkText("My Dashboard")).click();
or something else?
If you want to click on link in your example, you can use the selector you wrote, different kinds of css selectors (for example, By.cssSelector("#nav a") (looks for a link inside the "nav" list) or By.cssSelector("a[href='dashboard.action']") (looks for a link with specific href)) or using xPath selectors.
The important thing is to have a unique identifier to locate your element and an identifier that will fire 100% of the time.
For example, if you expect the link text to change on you, then don't look for that particular link text, because you have no guarantee that it will work 100% of the time.
Similarly, if there are 30 different elements that have the same id tag, don't use that either.
If things turn out to be very complex... that is, if you are in a large page with a lot of unknown variables, find by XPATH.
In the end, it really depends on the complexity of the website you are entering, and the goal of what you need done.
For more information, go to the Selenium javadocs and click BY on the sidebar for a list of different methods and how to use them.
If you need to click a link with an image, it would be better to locate the element with the explicit wait.
Example :
new WebDriverWait(driver, timeout).until(ExpectedConditions.presenceOfElementLocated(locator));

Selenium WebDriver - cssselector for locating input + onclick

I have the following checkbox:
<input id="outerLabor102" name="ctl02$chkSelectLabor" onclick="chkChildLabor(this);chkIntervallineChild(this);" type="checkbox">
I'm more of an xpath guy but since I want to come up with a selector that will find that checkbox by the input id PLUS the onclick attribute, I think a css selector should do just fine, but I can't find a way to formulate one.
I was thinking of something like:
(By.cssSelector("input[id=outerLabor102]input[onclick^=chkChildLabor(this);chkIntervallineChild(this);]")
Thanks for checking out my question.
I would not rely on the onclick attribute, but, for the sake of an example:
input#outerLabor102[onclick^=chkChildLabor]
where ^= is a starts-with notation.

Categories