I am facing one issue. I need to find one single any username but cant't able to locate element with any locators from those tags. I tried //*[#id="login_credentials"]/h4 or //[#id="login_credentials"]/text()1 but not able to extract any user_id from above list.
public class pr1
{
#Test
public void Test1() throws InterruptedException
{
WebDriverManager.chromedriver().setup();
WebDriver driver=new ChromeDriver();
driver.get("https://www.saucedemo.com/");
String str=driver.findElement(By.xpath("//*[#id=\"login_credentials\"]/text()[1]")).getText();
System.out.println(str);
}
}
All those texts: "standard_user", "locked_out_user", "problem_user" and "perfor" are text content of "//*[#id='login_credentials']" element. So, applying .getText() method will return all these strings.
You can extract all these values and after that to split it into substrings with regular split() method.
Related
I want to write a testmethod which I can give a parameter which will define which element to test.
Something like;
public void addImage(String imageNr){
$(By.xpath("(//input[#name='image'])['" + imageNr + "']"));
}
To get i.e. (//input[#name='image'])[2] or (//input[#name='image'])[3]
How would I go about that?
within Selenide you have something called the ElementsCollection. More information can be found on this page: https://selenide.gitbooks.io/user-guide/content/en/selenide-api/elements-collection.html
What you can do is transform the SelenideElement to an ElementsCollection by using double dollar signs:
For example:
This .get requires an Integer type. It will give you first all elements and you can take the second element from the returned list.
$$(By.xpath("(//input[#name='image'])).get(pageNr)
You will still need to do an action after getting this. for Example .click();
Good luck with it.
You can format the XPath String expression with the use of String.format, as following:
public void addImage(String imageNr){
String xpath = "(//input[#name='image'])[{0}]";
xpath = String.format(xpath,imageNr);
$(By.xpath(xpath));
}
I am taking the HTML code from website and then I would like to take the value "31 983" from attribute using Jsoup:
<span class="counter nowrap">31 983</span>
The code below is almost ready, but do not take this value. Could you please help me?:
public class TestWebscrapper {
private static WebDriver driver;
#BeforeClass
public static void before() {
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
driver = new ChromeDriver();
}
#Test
public void typeAllegroUserCodeIntoAllegroPageToAuthenticate() {
String urlToAuthencicateToTypeUserCode="https://www.test.pl/";
driver.get(urlToAuthencicateToTypeUserCode);
Document doc = Jsoup.parse(driver.getPageSource());
//how to take below value:
System.out.println(doc.attr("counter nowrap"));
}
#AfterClass
public static void after() {
driver.quit();
}
}
I was trying to use doc.attr, but does not help.
Jsoup uses CSS selectors to find elements in HTML source. To achieve what you want use:
// select the first element containing given classes
Element element = doc.select(".counter.nowrap").first();
// get the text from this element
System.out.println(element.text());
I'm afraid in your case there may be many elements containing classes counter and nowrap so you may have to iterate over them or try different selector to address directly the one you want. It's hard to tell without webpage URL.
Answering you original question, how to select by attribute:
Element element = doc.select("span[class=counter nowrap]").first();
or just:
Element element = doc.select("[class=counter nowrap]").first();
I want to click on an element which is from a list. I am using getAttribute("value") to get the text, but it returns 0, hence it is not clicking the element. Please help.
DOM structure
<ol class ="class1">
<li value="foo1" class="class2">
<li value="foo2" class="class2">
</ol>
Xpath:
#FindBy(xpath = "//ol[#class='class1']/li")
List<WebElement> tagList;
I have tried getText() but it returns the text along with unknown character, as the element has icons along with the text.
This is my code
public void selectTag() {
addReservationBtn.click();
tags.click();
for(WebElement li : tagList) {
// System.out.println(li.getAttribute("value"));
if (li.getAttribute("value").equalsIgnoreCase("foo2")) {
li.click();
break;
}
}
As text contains unknown characters so
li.getAttribute("value").equalsIgnoreCase("foo2")
will not work as both string not equal.
You can try contains like
li.getAttribute("value").contains("foo2")
I think reason for your problem may be
1. Wrong xpath or 2. Waits
// get second <li> using correct xpath
#FindBy(xpath = "//ol[#class='class1']/li[2]")
List<WebElement> tagList;
public void selectTag() {
addReservationBtn.click();
tags.click();
// waitforliTextToAppearAfterClick();
if (tagList.get(1).getAttribute("value").trim().equalsIgnoreCase("foo2")) {
tagList.get(1).click();
}
You are using #FindBy which only returns the first element found by the given locator. Use #FindBys to get all the elements found by the given locator.
As the previous answer was wrong I've striked it but kept it for
the clarification of the comments.
You can directly get the second element without using the for loop by
#FindBy(xpath = "//ol[#class='class1']/li[#class='class2']")
The getAttribute() method returns null if there are no attribute found and returns empty when there is an attribute but it doesn't contain any value.
unable to locate the hidden element in div
<div id="divDuplicateBarcodeCheck" class="spreadsheetEditGui" style="z-
index: 1200; width: 640px; height: 420px; top: 496.5px; left: 640px;
display:block"> ==$0
I want to locate the display element, but the element is hidden, i have written the code for it too.
String abc=d.findElement(By.xpath("//div[#id='divDuplicateBarcodeCheck']/"))
.getAttribute("display");
System.out.println(abc);
Thread.sleep(3000);
if(abc.equalsIgnoreCase("block"))
{
d.findElement(By.id("duplicateBarcodeCheck")).click();
System.out.println("duplicate barcode Close");
}
else
{ System.out.println("Barcode selected");}
There is no such attribute as display. It's part of style attribute.
You can either find the element and get its attribute style:
String style = d.findElement(By.xpath("//div[#id='divDuplicateBarcodeCheck']")).getAttribute("style");
if(style.contains("block")) {
d.findElement(By.id("duplicateBarcodeCheck")).click();
System.out.println("duplicate barcode Close");
} else {
System.out.println("Barcode selected");}
}
OR you can find this element directly with cssSelector (it's also possible with xpath):
WebElement abc = d.findElement(By.cssSelector("div[id='divDuplicateBarcodeCheck'][style*='display: block']"))
Note, that above will throw NoSuchElementException if the element was not found. You can use try-catch block to perform similar operations just like you did in if-else statement like this:
try {
d.findElement(By.cssSelector("div[id='divDuplicateBarcodeCheck'][style*='display: block']"));
d.findElement(By.id("duplicateBarcodeCheck")).click();
System.out.println("duplicate barcode Close");
} catch (NoSuchElementException e) {
System.out.println("Barcode selected");
}
If I'm getting you correct you are trying to archive checking if an element is displayed or not. You could do something like this using plain selenium and java:
// the #FindBy annotation provides a lazy implementation of `findElement()`
#FindBy(css = "#divDuplicateBarcodeCheck")
private WebElement barcode;
#Test
public void example() {
driver.get("http://some.url");
waitForElement(barcode);
// isDisplay() is natively provided by type WebElement
if (barcode.isDisplayed()) {
// do something
} else {
// do something else
}
}
private void waitForElement(final WebElement element) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOf(element));
}
Your Test (an end-to-end UI test!) should not stick to an implementation detail like display:none or display:block. Imagine the implementation will be changed to remove the element via javascript or something. A good selenium test should always try represent a real users perspective as good as possible. Means if the UI will still behave the same your test should still be successful. Therefore you should do a more general check - is an element displayed or not.
This is one of the basic functionalities of Seleniums WebElement interface, or to be even more precise its isDisplayed() method.
Quote from the Selenium Java Docs:
boolean isDisplayed()
Is this element displayed or not?
This method avoids the problem of having to
parse an element's "style" attribute.
Returns:
Whether or not the element is displayed
Furthermore I would recommend to write some small helper methods for things like that, in my experience it's a common use case you'll face more often.
helper method could for instance look something like this:
boolean isElementVisible(final By by) {
return driver.findElement(by).isDisplayed();
}
boolean isElementVisible(final WebElement element) {
return element.isDisplayed();
}
If you are using some Selenium abstractions like FluentLenium or Selenide things will become even more convenient because they provide things like assertion extensions and custom matchers for well known assertion libraries like assertJ, hamcrest, junit.
For instance with FluentLenium and AssertJ (a stack that i can personally recommend) the answer for your problem is looking as easy as this:
// check if element is displayed
assertThat(el("#divDuplicateBarcodeCheck")).isDisplayed();
// check if element is not displayed
assertThat(el("#divDuplicateBarcodeCheck")).isNotDisplayed();
Some more thoughts:
You should also use CSS selectors if possible instead of xPath selectors. CSS selectors are less fragile, it will speed up your tests and are better readable.
You should have a look at implicit waits instead of using Thread sleeps (bad practice). you can again implement helper methods like this by yourself, e.g:
void waitForElement(final WebElement element) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOf(element));
}
void waitForElement(final By by) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
void waitForElementIsInvisible(final By by) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.invisibilityOfElementLocated(by));
}
or (what i would recommend) use a library for that, for instance Awaitility
If your are looking for a more extended example you can have a look here:
java example with plain selenium
example using fluentlenium and a lot of other helpful stuff
Seems there is an extra / at the end of the xpath which you need to remove. Additionally, you need to induce WebDriverWait for visibilityOfElementLocated(). So effectively your line of code will be:
String abc = new WebDriverWait(d, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[contains(.,'Leave Balance')]//following::div[#id='applyleave_leaveBalance']"))).getAttribute("style");
System.out.println(abc);
if(abc.contains("block"))
{
d.findElement(By.id("duplicateBarcodeCheck")).click();
System.out.println("duplicate barcode Close");
}
else
{
System.out.println("Barcode selected");
}
Virtually, if() block is still an overhead and you can achieve the same with:
try {
new WebDriverWait(d, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[contains(.,'Leave Balance')]//following::div[#id='applyleave_leaveBalance']"))).click();
System.out.println("duplicate barcode Close");
} catch (NoSuchElementException e) {
System.out.println("Barcode selected");
}
Q1. I want to locate the display element, but the element is hidden, i have written the code for it too.
A1. As per your below code:
<div id="divDuplicateBarcodeCheck" class="spreadsheetEditGui" style="z-
index: 1200; width: 640px; height: 420px; top: 496.5px; left: 640px;
display:block"> ==$0
It doesn't look hidden, the problem is that your using incorrect xpath and element getter.
Use:
String abc = d.findElement(By.xpath("//div[#id='divDuplicateBarcodeCheck']"))
.getCssValue("display");
Instead of:
=> .getAttribute("display");
Alternative method using JavascriptExecutor:
JavascriptExecutor jse = (JavascriptExecutor) d;
String displayProperty = (String) jse.executeScript("return
document.getElementById('divDuplicateBarcodeCheck').style.display");
System.out.println("Display property is: "+displayProperty);
I am new on automation testing and have difficulty when trying to pratice using selenium 3 on booking.com website
There is auto suggestion text box, when you type word, shown auto suggestion and you can click from the list i.e Downtown Singapore
Have try with xpath id("basiclayout")/div[#class="leftwide rilt-left"]/div[#class="sb-searchbox__outer"]/form[#id="frm"]/div[#class="sb-searchbox__row u-clearfix"]/div[1]/div[#class="c-autocomplete
sb-destination"]/ul[#class="c-autocomplete__list sb-autocomplete__list -visible"]/li[#class="c-autocomplete__item sb-autocomplete__item sb-autocomplete__item--city sb-autocomplete__item__item--elipsis"]
or css c-autocomplete__item sb-autocomplete__item sb-autocomplete__item--city sb-autocomplete__item__item--elipsis
all scenario failed when i run my testcases on selenium java
How to handle on such web element?
Complete code:
public class Selenium3Testing {
private WebDriver driver;
#Before
public void setUp() {
String baseUrl = "https://www.booking.com/";
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
driver = new ChromeDriver(capabilities);
driver.get(baseUrl);
}
#After
public void tearDown() {
driver.quit();
}
#Test
public void openBookingDotCom() {
driver.findElement(By.id("ss")).click();
driver.findElement(By.id("ss")).clear();
driver.findElement(By.id("ss")).sendKeys("Singapore");
//click on auto suggestion row number 2
driver.findElement(By.css("c-autocomplete__item sb-autocomplete__item sb-autocomplete__item--city sb-autocomplete__item__item--elipsis")).click();
}
}
I just typing from mobile, so no code, here the way we can do it.
For giving input to input box, I hope if we pass total word in sendkeys, suggestion's may not load or delayed. So best way I follow is pass each character..may be sleep say 300 milli sec for each char. Write as small method which will loop for all chars in word.
To click on suggestion list, try for xpath contains text..or any one works well.