Insert value into input field using Selenium and Java - java

I'm using the line of code:
driver.findElement(By.xpath("//*[#id=\"sernum\"]")).sendKeys("XXXXXXXXXX");
All I need to do is insert a value in the field. But it isn't work...
The fragment from "dom":

To send a character sequence within the <input> field as the element is a dynamic element you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#sernum[name=\"sernum\"]"))).sendKeys("XXXXXXXXXX");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='sernum' and #name=\"sernum\"]"))).sendKeys("XXXXXXXXXX");

Related

Selenium: How to get all the h3 text with getText()

In the webpage https://cloudwise.nl/dit-is-cloudwise/alle-cloudwisers/directie/ I'm trying to get all users' names using for loop.
What I have tried so far is:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'inner')]/h3"))).getText();
and
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'inner')]/h3"))).getAttribute("innerHTML");
But all of them gets text Directie instead of users' name. I think it's because of the users' name is in a header <h3> tag and it just ignores it. How can I get the users' name within a header tag?
You were close enough. visibilityOfElementLocated() will always return you the first matching element, where as you need all the matching elements.
Solution
To print the list of users you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("div.inner h3"))).stream().map(element->element.getText()).collect(Collectors.toList()));
Using xpath:
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[contains(#class,'inner')]//h3"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList()));

Selenium: No such element exception is getting on this http://www.salesfoce.com/ even thought that element exists

I am trying to find an element By.cssSelector("#e1"). On this website : SALESFOCE.COM.
I am trying to find an element on this first button(image below), which is a <div>.
From java, I tried By.xPath and By.cssSelector but every time I am getting No such element exception.
I also used Implicit and explicit wait still getting same exception.
But if I find using inspect element than it highlights the element.
The element Salesforce is under iframe you need to switch to iframe first in order to access the element.
Use WebdriverWait() and wait for frameToBeAvailableAndSwitchToIt() and use following locator to identify.
Use WebdriverWait() and wait for elementToBeClickable() and use following locator to identify.
driver.get("http://ww1.salesfoce.com/")
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("#rs>iframe")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Salesforce']"))).click();
The element with the text as Salesforce is within an <iframe>
Solution
To click on the element with text as Salesforce within the website you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using xpath:
driver.get("http://ww1.salesfoce.com/");
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#src, 'https://www.google.com/afs/ads?adtest')]")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Salesforce']"))).click();

Selenium: How to get text out of a div

I want to get a text which is inside a div. I tried to find help from other questions, but most of them say .getText() which doesnt exist anymore.
This is the inspect:
Im using a chrome extension with which i can copy out the XPath, but it doesnt reads the text inside the div. It copies me this here:
//div[#class='mud-alert-message']
To print the text Die Datei... you can use either of the following Locator Strategies:
Using cssSelector and getAttribute("innerHTML"):
System.out.println(wd.findElement(By.cssSelector("div.mud-alert-message")).getAttribute("innerHTML"));
Using xpath and getText():
System.out.println(wd.findElement(By.xpath("//div[#class='mud-alert-message']")).getText());
Ideally, to extract the text you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.mud-alert-message"))).getText());
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='mud-alert-message']"))).getAttribute("innerHTML"));
I believe is something like this:
WebElement element = new WebDriverWait(driver, ...).until(ExpectedConditions.elementToBeXXX(By.LOCATOR_TYPE));
String text = element.getText();

How to get value between ::before & ::after?

I would like to get value from text between ::before ::after in selenium. I suppose it's impossible to do it by xpath, what JSexecutor code I can use?
Value I would like to get:
::before and ::after are the css and are not printable text. So to print the text Widget you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(wd.findElement(By.cssSelector("div.linkbox.margin-bottom-20 > h1.heading")).getText());
Using xpath:
System.out.println(wd.findElement(By.xpath("//div[#class='linkbox margin-bottom-20']/h1[#class='heading']")).getText());
Ideally, to extract the text Some Text as the element is a dynamic element, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.linkbox.margin-bottom-20 > h1.heading"))).getText());
Using xpath:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='linkbox margin-bottom-20']/h1[#class='heading']"))).getText());

How to handle the following dynamic ID through Selenium and Java?

Would you mind helping me to understand how I can handle this dynamic ID?
Here are cases which I have already tried:
driver.findElement(By.xpath("//input[contains(#id,'Username')]")).sendKeys("aaa");
driver.findElement(By.xpath("//input[starts-with(#id,'undefined-undefined-Username-')]")).sendKeys("aaa");
driver.findElement(By.xpath("//*[#type='text']")).sendKeys("aaa");
No way to find that element.
As per the HTML you have shared the element is a dynamic element . To invoke click() on the desired element you can use either of the following solutions:
cssSelector:
driver.findElement(By.cssSelector("label[for^='undefined-undefined-Username-']")).sendKeys("aaa");
xpath:
driver.findElement(By.xpath("//label[starts-with(#for,'undefined-undefined-Username-')][contains(.,'Username')]")).sendKeys("aaa");
Update
As the element is dynamic you may need to induce WebDriverWait for the desired element to be clickable as follows:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[for^='undefined-undefined-Username-']"))).sendKeys("aaa");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[starts-with(#for,'undefined-undefined-Username-')][contains(.,'Username')]"))).sendKeys("aaa");

Categories