Selenium webdriver java query - java

Please provide me the automation script for the following select country drop down box.

You can select drop down values in different way :
Visible Text
Index
Option values
Please find below example using java code
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.automationtesting.in/Register.html");
driver.manage().window().maximize();
Select drpCountry = new Select(driver.findElement(By.xpath("//select[#id='countries']")));
drpCountry.selectByVisibleText("Hungary");
drpCountry.selectByValue("India");
drpCountry.selectByIndex(1);

country=Select(dr.find_element_by_id("country"))
allCountries=country.options
for count in allCountries:
print(count.text)
country.select_by_visible_text("Japan")

Related

java driver in selenium project

I am using simple project, where you fill in the form. The last part is to click on a element with no id and no text.
I found that i could use java to do it, but how do i initiate the driver in selenium?
Actions builder = new Actions(driver);
builder.moveToElement(knownElement, 10, 25).click().build().perform();
I tried
WebDriver driver = New ChromeDriver();
but that only raised a lot new problems. I am using IntelliJ.
You need chromedriver.exe to initiate selenium web driver.
First download the ChromeDriver. You need to download ChromeDriver for your respective Operating system from this link
//Then set the download location.
System.setProperty("webdriver.chrome.driver", "C://Selenium-java//chromedriver_win32//chromedriver.exe");
//Creating an object of ChromeDriver
WebDriver driver = new ChromeDriver();
//launching the specified URL
driver.get("https://www.google.com/");
Then find your element to be clicked using xpath.
If you say you don't have any id or text for click then use any parent element as reference which you can identify then use xpath axes to identify it.
Then you can click on that element directly.
driver.findElement(By.xpath("//form[#id = 'id']/child::button")).click;

How to pass a headless option for my driver using Java and Selenium?

I am setting up a chrome driver with the help of Selenium and Java. I want this driver to be executed headless but I can't find out the way. Can you explain to me what do I need to do?
My code sample:
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICITY_TIME, TimeUnit.SECONDS);
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH); // OS and Browser options
ChromeOptions options = new ChromeOptions(); // create options instance
options.addArguments("--headless"); // add an option
driver = new ChromeDriver(options); // create a driver with the specific options instance
You just need to create a ChromeOptions object in which you need to save the options for your own driver.
To add your own options just use this: options.addArguments(); and in the parenthesis insert your option in string mode.
For more details and documentation please also check here:
http://chromedriver.chromium.org/capabilities
I think this is going to work.

How to select the location from a field which fetches result from google map api using selenium webdriver(Java)

I have tried entering the complete field value using selenium but this doesn't work. It requires to select from the suggestion shown. Anyone knows how to select from the suggestion show.
Note: I am using hybrid framework.
something like the below one? If yes, replace Thread.sleep with explicit waits.
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.co.in/maps?source=tldso");
Thread.sleep(3000);
driver.findElement(By.id("searchboxinput")).sendKeys("Kora");
Thread.sleep(3000);
List<WebElement> autoSuggestions=driver.findElement(By.className("sbsb_b")).findElements(By.tagName("li"));
for (WebElement suggestions : autoSuggestions) {
if (suggestions.getText().contains("Koramangala Police Station")) {
suggestions.click();
Thread.sleep(3000);
break;
}
}
driver.quit();
Just use sendKeys(Keys.Arrow_Down);
until the address you need and then use
sendKeys(Keys.Enter);

Can I test all 3 combinations in a drop down using Selenium Webdriver on JAVA platform?

This what I have for a single select option at a time
WebElement address = driver.findElement(By.id("package-rooms"));
Select ab=new Select(address);
ab.selectByVisibleText("1");
But once the above code is done, i want to execute the 2nd and 3rd options. I don't want write separate test cases. I am pretty new in selenium. Please help....Thank you!
You can use below code if you want to select all the options of dropdown:
WebElement address = driver.findElement(By.id("package-rooms"));
Select ab=new Select(address);
List<WebElement> list = ab.getOptions();
for(Webelement ele : list){
ab.selectByVisibleText(ele.getText());
}
Hope it helps.

Test autocomplete with Selenium webdriver

I have a text box in which when I type one letter say 's' , it displays a list of results ( like google search) .
I am using latest selenium webdriver with java.
I have tried
sendKeys("s"),
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onkeyup");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onblur");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onclick");
jsLib.callEmbeddedSelenium(driver, "doFireEvent", driver.findElement(By.id("assetTitle")), "onmouseup");
driver.findElement(By.id("assetTitle")).sendKeys(Keys.ENTER);
None of these work even after adding wait after each of the steps.
Any suggestions?
Thanks.
Update :-
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement query = driver.findElement(By.name("q"));
query.sendKeys("s");
driver.findElement(By.xpath("//table[#class='gssb_m']/tbody/tr/td/div/table/tbody/tr/td/span")).click();
driver.findElement(By.name("btnG")).click();
Update 2 : -
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
WebElement query = driver.findElement(By.name("destination"));
query.sendKeys("s");
Update 3 :-
I tried with Selenium 1 and the fireevent method works by passing parameter as 'keydown'. This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");
I found a workaround about this. My problem was:
Selenium inputted 'Mandaluyong' to an auto-suggest location field
The auto-suggest field appears together with the matched option
Then selenium left the auto-suggest drop-down open not selecting the matched option.
What I did was:
driver.findElement(By.name("fromLocation")).sendKeys("Mandaluyong");
driver.findElement(By.name("fromLocation")).sendKeys(Keys.TAB);
This is because on a manual test, when I try to press TAB key, two things were done by the system:
Picks the matched option from the auto-suggest drop-down
Closes the auto-suggest drop-down
I believe you are testing auto-suggest here (not auto-complete)
Steps I follow -
Enter something in the input field
Click on the suggestion you want to choose. (You can find the xpath using some tools like Firebug with Firepath, Chrome, etc.)
Verify the text in the input field is same as expected.
This should be a temporary workaround for now.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kayak.com/");
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www.kayak.com/");
sel.type("//input[#id='destination']", "s");
sel.fireEvent("//input[#id='destination']", "keydown");

Categories