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
Related
I want to send a text to a div element in web.whatsapp. I tried several ways ie using XPath, cssSelector, class name etc. but none is working for me.
This textbox comes when we attach an image in web.whatsapp.
Please step into the div and use the div with the class "selectable-text". As this is a contenteditable div you should be able to click into it and call then sendKeys() onto the element.
if this does not help please let me know then I will look in our code how we are filling such divs exactly.
You need to drill down further until you get to a web element that takes an input.
I try to click in a button like imagen.
not working using class or xpath
this is the button
this is the inspect from this button.
this is the code trying to click on the button:
driver.findElement(By.xpath("/html/body/div[10]/button")).click();
this is the xpath from the before:
driver.findElement(By.xpath("/html/body/div[10]"));
please could someone help me!!!
Every simple change in the page will cause your code to stop functioning, try to always make use of class or id and navigate to it's siblings/parent nodes.
You can do that in 2 ways
1.By using CssSelector (Right click on the Element in DevTools -> Copy -> Copy Selector)
driver.FindElement(By.CssSelector("CopiedText")).Click()
2.By using XPath and accessing it through it's parent (Example for your case)
driver.FindElement(By.xpath("//div[#class='advertising-layer']/button")).Click()
I experienced the similar issue while accessing a button in a dialog box. I tried with XPath, id, it didn't work but it worked with CSS selector.
By using CSS selector I made the selenium webdriver to click on a button, pick a value from the drop-down in the dialog box, type a value in the text box.
I am not sure why exactly it worked with CSS selector and not with XPath. I'd be grateful if someone has a description for this.
If you want to get CSS selector of a particular element follow the below steps
Open the application in Firefox
open Inspect element, and in the inspector ( the place where you see the HTML code)
Select the element in the page to view the particular code for the inspector by using this button
right click on the highlighted part of the code and hover on copy, you will find CSS selector
Let me know if this works out for you.
As per the HTML you have shared to invoke click() on the desired element you have to induce WebDriverWait and you can use the following solution:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/div[#class='advertising-mask']//button"))).click();
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.
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
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.