I'm just trying to find out how to tell java to open a website and enter text (preferably a string value) into a text field
For example go to Google and search any text( it does not have to be user entered)
I realize that it wont actually open any browser or print anything from a website. I just need to know this basic part to build on for my program.
If you try to query to Google in your example and want to get the search result, you can use query string and read its html result
Figured it out . I had to use a combination of Selenium and HtmlUnit. My code is something like this
WebDriver driver = new HtmlUnitDriver();
driver.get("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Hello");
driver.quit();
Related
I am doing automation using selenium webdriver (java) on a search engine BookMyCrop (http://www.bookmycrop.com). Here, I searched for a crop but, I am not able to click on desired search result. Please help me with it.
Code :
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
------My 1st try-------------
//WebElement link = driver.findElement(By.xpath("\"//div[#id = 'Links']/a[3]\""));
//link.click();
------My 2nd try-------------
//List<WebElement> find = driver.findElements(By.xpath("/html/body/section[2]/div[2]/div/div/div/div[1]"));
//find.get(1).click();
}
} –
You can use the css selector based on class names: ".product-block.inner-product-block" and get the list of all the search results.
Then click on whatever index you want to click.
I am not using an IDE for this but it would look something like this:
driver.findElements(By.cssSelector(".product-block.inner-product-block")).get(0).click();
As said, you can try with css ".product-block.inner-product-block"
Then
get List of WebElements
do loop
inside loop, try get text of each element or innerText attribute
cross check if it is required one or not by simple if condition
If so, click on that web element and break loop
if this locator is not giving required info, try other locator. say $$("a h3") for veg names.
The below code worked for me. It is navigates to the correct link
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.bookmycrop.com");
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
Hello I have tried the following link.
It is working only if we select the text manually.
But my case is like, we have found the text using the text search.
Is it possible to find the selected text using selenium web driver??
We can do this by using getCssValue method available in WebElement.
Demo:
WebElement branch = driver.findElement(By.xpath(".//h3[contains(text(),'locator')]"));
String background = branch.getCssValue("background");
Assert.assertEquals(background, "We know the actual bg color");
My requirement is I want to select the particular name from the auto-suggestion in the autocomplete text box.
So here I only know how to get the name by using the mouse down to achieve this But I know it's not a good solution to get that because we don't give the guarantee to the auto-suggestion is same as all the time in the browser.
So if anyone knows how to get the auto-suggested text names for the auto-complete text box in Selenium Web Driver using Junit (Here I am using the Junit in Selenium WebDriver to develop the automation test script).
My code:
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ENTER);
Here the above code is only working for my correct option is shows as
the second option of the auto-suggested texts.
So that's why I need how to get the text names in the auto-suggestion for the autocomplete text box.
Please the give the solutions as the JUnit Form to my question because I am using the JUnit to develop the automation test script.
Thanks
The auto-suggest box is HTML like anything else on the page. Create a locator that finds the auto-suggest panel and then parse the contents. Once you figure out the structure, you can get the text that you need and then assert that it is correct.
I can't see the full HTML from your screenshot but you can see that the list is contained in an HTML UL. The UL is the container and each LI is a list item in the dropdown. You can use that info to do something like
ul.autoCompleteGroup > li
to get all the list items. I can't see what's inside of there but at some point you should be able to use .getText() to get the auto suggest items. From there you just compare what you pulled off the list to what you are expecting with an Assert.
Please try below code
public void selectAutoCompleteItem(String itemText) {
String xpathPattern = "//div[#id='phSearchInput_autoCompleteBoxId']" +
"//ul/li/a[.='%s')]";
String xpathExp = String.format(xpathPattern, itemText);
driver.findElement(By.xpath(xpathExp)).click();
}
In that case you can use the findelements functionality. So you can say:
List<WebElement> elements = driver.findElements(by.xpath("//a[#class='autoCompleteRowLink']");
And then for
each list item you can use getText to get the exact text. Then you can assert it with the expected values
Please check out the element of this website.
It has a form, and along with 2 text input and 1 submit button.
I dont know which one from those 2 inputs that is actually used when the user type-in some urls over there.
But when I tried this (using firefoxDriver) to get the element:
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's unable to locate the element.
Then I change my code to this :
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
WebElement textfieldURL = driver.findElement(By.id("ping-url")); // even ping-box not working
The result's still unable to locate the element.
Any clues?
You haven't mentioned the exception which you are facing. As your input tag present under iframe so you need to first switch into frame and than have to perform actions -
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
//or you can use frame index as well
driver.switchTo().frame(0);
your element is available with the id ping-box . Try the following complete code -
System.setProperty("webdriver.gecko.driver","D:/Application/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.twingly.com/ping");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.className("ping-iframe")));
driver.findElement(By.id("ping-box")).sendKeys("http://www.google.com");
driver.findElement(By.id("ping-button")).click();
Same is working for me.
In our web-aplication we use several alerts like this:
I need to retrieve link from this alert.
I did:
Alert alert = driver.switchTo().alert();
String link = alert.getText();
But alert.getText() returns "Link to copy" not a link in textfield
so how can I get link in this case?
WebDriver will be unable to retrieve the link text.
For Windows, you could leverage the Windows Automation API to scrape the text, other operating systems probably have similar APIs that you could utilize - but the short answer is that WebDriver won't be able to do this.