Send text to div in Selenium + Java - java

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.

Related

CSS Selector: Anchor text of href contains

I am currently working with Selenium and have now reached the interesting, yet incredibly difficult, world of CSS selectors.
I'm currently looking at selecting the different options of the Google tool bar. E.g when you search for something, on the results page you get options to search for the same term but under images, news, videos etc
I'm particularly interested in selecting the "Images" link.
I've been working on it for quite a while and the closest i have got is the below selector:
div a.q.qs[href]
This drills down into the right sub classes but there are 16 of them. Despite hours of aimless searching, i'm unable to complete the query with a contains method around the anchor text, which is unique in the targeted sub classes.
I'm aware there is a By LinkText option in Selenium, but i'm not sure if the anchor text is unique across the entire page. Also, i really want to understand CSS selectors in general so even if it was, i want to resolve this particular issue so i can apply it to future problems.
I'm looking for something like the below pseudo CSS selector:
div a.q.qs[href].Anchorcontains("Images")
Can anyone help?
All links have a unique parameter called tbm: its value is isch for images, so I'd go with
a[href*="tbm=isch"]
There are sometimes ways to get what you want with CSS selectors but if you want to find an element by contained text, you will have to use either link text/partial link text if it's a link or XPath for everything else.
The XPath for what you want is
//div[#id='hdtb-msb-vis']//a[.='Images']
You could use //a[.='Images'] but that returns two elements, one of which is not visible.
To break this down
// at any level
div find a DIV
[#id='hdtb-msb-vis'] that contains an ID of 'hdtb-msb-vis'
//a that has a child A at any level
[.='Images'] that contains text (.) equal to 'Images'
If you want to explore by link text, you can write something like
int count = driver.findElements(By.linkText("Images")).size();
and print count. My guess is that it will be 2, one of which is not visible. You can use Selenium to further filter this down to only the visible link, if you wanted.
You are going to have the same issue with BackSlash's CSS selector answer. You can tweak it slightly and solve this problem with the CSS selector locator
#hdtb-msb-vis a[href*='tbm=isch']
Here are some links to CSS references that will help you get started: W3C reference, SauceLabs CSS Selectors tips, and Taming Advanced CSS Selectors.

How to click on a button inside a popup as per the html through SeleniumWebdriver and Java

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

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 retrieve Textbox Value In Selenium Testing

I am new to Selenium testing, I am trying to put the value into the textbox with the help of xpath, name & id. (java using eclipse IDE):
driver.findElement(By.xpath(".//* [#id='txtEmpDepAddressLine1']")).sendKeys("2nd Main")
driver.findElement(By.xpath(".//*[#id='txtEmpDepAddressLine2']")).sendKeys("Bangalore");
While running, textbox is not retrieve the from the above code. Can anyone help me out to solve this problem?
Use By.id instead By.xpath
If you insist xpath then:
Start with: // instead: .//
Make sure the id value is unique on page.
if using xpath in some browsers id or ID makes differnce.
It is difficult to say without the HTML, but I'd suggest a few things:
Open your page, go to the Google Chrome DevTools console, and verify you can locate the element using your xpath. So you'd type:
$x("//* [#id='txtEmpDepAddressLine1']")
and verify the text box is located.
If the sendKeys isn't working on the element at that id, I'd wonder if it is not on the text field itself, but on a containing table or something like that. Make sure the element located by the id is the input, so the sendKeys will work.
By
In the text box it is fetching the value but it can't able not store
in the text box field
do you mean you can see selenium typing those values into the textbox and then the values disappear?
If that is what is happening, it is possible there is some javascript running on the page while selenium is entering the values in the textbox. you can try to put a wait before selenium enters the value. If waiting resolves the issue, you will need to write a method to detect when the javascript is done.

Is it possible to enter a text in a textbox without specific Element ID

Im doing a test that enter text in the textbox but it dont have specific ID so everytime i run the test it will change. im using selenium webdriver in java please help
The following can work. You can google for them and see how they work.
driver.findElement(By.id("id"));
driver.findElement(By.cssSelector("cssSelector"));
driver.findElement(By.name("name"));
driver.findElement(By.linkText("linkText"));
driver.findElement(By.partialLinkText("partialLinkText"));
driver.findElement(By.className("className"));
driver.findElement(By.xpath("xpath"));
I am sure some of them will be useful. Please let me know if you want more info.
The easiness of using them is in the order which i have mentioned.
What means doesn't have a specific ID?If id at least partially remains the same you can use the CSS locator
driver.findElement(By.cssLocator("input[id*=somePartWhichNotChange]"));
//* star means contains
if not, then you can use cssSelector to get to your element like "body table input" or use xpath as a last resort.

Categories