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.
Related
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();
Kindly help me to read Text of the element which I'm trying to access using x-path (I tried absolute and partial x-path but could not able to read value. below is my code. Getting message -"INFO: Detected dialect: W3C"
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class xpathPractice {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\BrowserDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
driver.get("http://www.lavasa.com/learn/acca.aspx");
String str3 = driver.findElement(By.xpath("//*[#id='main-nav']/ul/li[4]/ul/li[1]/a")).getText();
System.out.println(str3);
//After executing this code, I see the line in console as "INFO: Detected dialect: W3C"
}
}
the element you are trying to find is actually hidden, which is revealed on mouseover. So, we'll have to first make the web element visible, only then you can use the getText() function.
Step 1: Identify the web element you want to mouseover:
WebElement ele = driver.findElement(By.xpath(".//*[#id='main-nav']/ul/li[4]/a"));
Step 2: Use the Actions Class to moverover the webelement:
Actions act = new Actions(driver);
act.moveToElement(ele);
act.build().perform();
Step 3: Now, once the element is visble, go ahead and use getText() to get the text of the element.
String str3 = driver.findElement(By.xpath("//*[#id='main-nav']/ul/li[4]/ul/li[1]/a")).getText();
System.out.println(str3);
You are trying to gettext from a webelement which is not displaying on the current screen.
We can getText by two ways.
Using Javascript executor
System.setProperty("webdriver.gecko.driver", "C:\\BrowserDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("http://www.lavasa.com/learn/acca.aspx");
WebElement element = driver.findElement(By.xpath("//*[#id='main-nav']/ul/li[4]/ul/li[1]/a"));
String script =(String)js.executeScript("return arguments[0].innerHTML;",element);
System.out.println(script);
2.Using actions mouse hover the element and then use getText.
Instead you can use getAttribute() method of selenium as follows:
driver.findElement(By.xpath("//*[#id='main-nav']/ul/li[4]/ul/li[1]/a")).getAttribute("innerHTML");
I'm new to coding and I have the following issue while automating with Selenium using Java:
I'm testing a SaaS solution and I need to refresh my page until a certain item appears in my inbox. I cannot simply use the findelement statement since the item only appears after some time and onlywhen the page has been refreshed. Furthermore all inbox items have a unique sequential number in the title. This unique number I have saved as a string variable and I want to use this string variable to see if the inbox item has appeared after refreshing the page some times.
Code for finding unique string:
//Get expense report number
String filename = strng;
String ExpenseReportNumber = StringUtils.substringBefore(filename, " submitted"); // returns Expense Report #XXXX
Now I need to create a loop in which the site keeps refreshing until it gets a hit on the string variable. When it finds the string variable I then can select the top element on the inbox and continue my test.
Can someone assist me with this issue or give me advice on how to create the same outcome but then with a better approach? Much appreciated!
Wait<WebDriver> wait12 = new FluentWait<WebDriver>(driver)
.withTimeout(600, TimeUnit.SECONDS)
.pollingEvery(15, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement ToDoExpense = wait12.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
driver.navigate().refresh();
return driver.findElement(By.partialLinkText(ExpenseReportNumber));
}
});
I have found the solution myself
public WebDriverWait wait = new WebDriverWait(driver, 10); //throws exception if element is not found within 10 seconds
driver.navigate().refresh();
wait.until(ExpectedConditions.presenceOfElementLocated(By.partialLinkText(ExpenseReportNumber)));
//continue your test
I worked it out like this:
do
{
driver.refresh();
driver.waitForPageToLoad();
driver.wait(1000);
} while (driver.findElements(By.locator(element)).size() < 1);
I am trying to dynamically search an "li" tag item and double-click on this website: www.jstree.com (the right-top hierarchy tree sample). The code does find the WebElement but does not do anything. I am trying as follows. Can someone please point to what I am doing wrong? I am using Firefox 35.0.1 and selenium 2.44.0.
driver.get(baseUrl + "http://www.jstree.com/");
WebElement we = driver.findElement(By.xpath("/html/body/div/div/div[1]/div[1]/div[2]/div[1]/ul/li[1]/ul"));
Actions action = new Actions(driver);
List<WebElement> liItems = we.findElements(By.tagName("li"));
for(WebElement liItem:liItems)
{
System.out.println(liItem.getText());
if(liItem.getText().startsWith("initially open"))
{
System.out.println("Found it...");
liItem.click();
action.moveToElement(liItem).doubleClick().build().perform();
break;
}
}
I ended up doing this:
Modified the selector to make sure ONLY the expected elements are returned. It helps a lot in terms of execution time and reducing the number of unwanted loopings. And, then find the element in run time and use Action() on that to perform double click. I also update the Selenium binding as #alecxe suggested to work with latest Firefox
public void DemoTest() throws InterruptedException {
List<WebElement> liItems = driver.findElements(By.xpath("//*[contains(text(),'initially open')]"));
for(WebElement liItem:liItems)
{
Actions actions = new Actions(driver);
actions.moveToElement(liItem).doubleClick().build().perform();
}
}
Im trying to automate the Google Images page:
https://www.google.com/search?q=pluralsight&biw=1416&bih=685&source=lnms&tbm=isch&sa=X&ei=qGd6VN6bEZTooAT7q4C4BQ&sqi=2&ved=0CAgQ_AUoAw
All the images have the same class but no id and the results are constantly changing. So I would like to be able to click on the images based on their index.
I know how to do it in C#...but I cant figure out how to specify in the index in Java. When I try to select an index beyond 0, I get and IndexOutOfBounds error, but i cant figure out why
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Here is the entire code im using...any help would be appreciated:
System.setProperty("webdriver.chrome.driver", "/Users/user/chromedriver");
WebDriver chromeDriver = new ChromeDriver();
chromeDriver.get("http://www.google.com");
WebElement searchBox = chromeDriver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
chromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement imagesLink = chromeDriver.findElement(By.linkText("Images"));
imagesLink.click();
WebElement image = chromeDriver.findElement(By.className("rg_di"));
WebElement imageLink = image.findElements(By.tagName("a")).get(1);
imageLink.click();
Any help would be greatly appreciated
In your code:
WebElement image = chromeDriver.findElement(By.className("rg_di"));
will return the first element found on the page with a class of "rg_di".
That element has only one <a href=... /a> tag in it.
You are getting an IndexOutOfBounds exception because you are asking for the second one (zero based indexing). If you change your final WebElement to:
WebElement imageLink = image.findElements(By.tagName("a")).get(0);
The code should work for you with that small change.
This is my quick version (note the lack of storing elements I only need to do one thing with as WebElements):
public static void main(String[] args) {
// I don't have Chrome installed >.<
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.id("gbqfq"));
searchBox.sendKeys("pluralsight");
searchBox.sendKeys(Keys.RETURN);
driver.findElement(By.linkText("Images")).click();
WebElement image = driver.findElement(By.className("rg_di"));
image.findElements(By.tagName("a")).get(0).click();
// super-shortened version:
// driver.findElement(By.className("rg_di")).findElements(By.tagName("a")).get(0).click();
}
I'd do:
List<WebElement> we = chromeDriver.findElements(By.cssSelector(".your-class a"));
we.get(1) //should get first element in array
This code worked very well when we have similar object properties for same web buttons, then using
List<WebElement> we = webdriver.findElements(By.cssSelector(""));
and then getting
we.get(1).click();
Thank you so much for posting this answer.
Another solution may be like this:
If the class name and the index of the web element are known, then following code works:
driver.findElement(By.xpath("(//*[#class='android.widget.ImageView'])[18]")).click();