I am trying to write a code to find cheapest tickets between two locations .I am stuck in the beginning of the code because selenium does not recognise simple input text box . here is the code .
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class orbitzsimpleTest {
public static void main(String args[]) throws InterruptedException {
WebDriver dr = new FirefoxDriver();
dr.navigate().to("https://www.orbitz.com/");
Thread.sleep(5000);
System.out.println("after threat sleep");
dr.findElement(By.xpath(".//*[#id='flight-origin']")).sendKeys("SFO");
}
}
I also tried to write the XPath using contains like
List<WebElement> s = dr.findElements(By.xpath(".//*[contains(#id,'flight_*')]"));
No luck. It looks so simple but i'm struggling to go past this.
You can just get this element with dr.findElement(By.id('flight-origin')) or (By.cssSelector('#flight-origin')).
And it's better to use 'smart' waits (wait for element to be displayed/clickable/visible) instead of sleep. Because it can be that 5 secs is not enough and you try to send key to the input which isn't loaded yet.
I think i found the reason why it was not recognising that element. It's because the flight tab had to be selected before it finds this text box .I was able to fix it .
Thank you so much for your input .
Related
I am new to Selenium webdriver, and trying to test a webpage which has some dynamic links (dynamic ads). Example : ads on https://mail.rediff.com/cgi-bin/login.cgi
I tried with xpath ,classname and id but none of these is working.It is happening because every time new content is displaying on the page so it is not able to find the element and throwing Exception in thread "main":
org.openqa.selenium.NoSuchElementException: Unable to locate element: #map.
my code is :
package Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Image_Link {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver", "E://chromedriver_win32//chromedriver.exe");
//driver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver", "E://geckodriver-v0.21.0-win64//geckodriver.exe");
driver = new FirefoxDriver();
Actions actions = new Actions(driver);
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
//WebElement Dynamic_ads=driver.findElement(By.className("rhs-area floatR"));
WebElement Dynamic_ads=driver.findElement(By.id("map"));
actions.moveToElement(Dynamic_ads).perform();
WebElement ad_Link = driver.findElement(By.cssSelector("#map > area:nth-child(2)"));
actions.moveToElement(ad_Link);
actions.click();
actions.perform();
//driver.navigate().to("www.google.com");
//String value = driver.findElement(By.id("hplogo")).getAttribute("title");
}
}
The elements for the dynamic ads cannot be found because it isn't fully loaded yet on load page. I recommend adding an explicit wait time for the specific add you're looking for. You can check out this link here for more information.
Anyhow, here's your solution:
1.)Implement WebDriverWait: WebDriverWait wait = new WebDriverWait(driver, 20);
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("map"))); then do Dynamic_ads.click(); afterwards.
OR
2.) Change your Dynamic_ads data to wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//map[#id='map']/*"))); then do Dynamic_ads.click(); afterwards. NOTE: This will pick the first child node.
By using this code i can click one suggestion from all suggestions. I cannot click the suggestions randomly. I need to click one suggestion randomly and verify only the clicked the suggestion is displayed or not.
package google;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Google {
public WebDriver driver;
#Test(priority=1)
public void Firefoxaccess() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:/Users/naveenkumar.d/Downloads/geckodriver-v0.17.0-win64/geckodriver.exe");
driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("n");
Thread.sleep(3000);
driver.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li[1]/div/div[2]")).click();
}
}
You could compose a selector based on a rand method.
For example:
r = generate random number here
and the selector should be something like:
//ul[#role='listbox']/li[r]
As for how to check that the option was selected you need to check that the search input has your text.
Text should be what you typed concatenated with the text from your random option, meaning the text from //ul[#role='listbox']/li[r]//b
And the selector for the check: //input[#name='q'][contains(#value, 'your_text')]
I am beginner for Selenium, just I want to check that search of the website working properly. Means when I enter any keyword in the search box and click on search is it provides correct search result. How to Check using Selenium WebDriver. Please guide me.
You should provide more details about your query
for example you should include the URL your are working on or a similar URL for the one you are working on
You can use an item property from the search result page as an assertion point for your test
Try below code
package StackOverFlow.StackOverFlow;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class App
{
#Test
public void test () throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\Chrome\\2\\chromedriver.exe");
WebDriver Dr = new ChromeDriver();
Dr.manage().window().maximize();
Dr.get("http://www.conns.com");
Thread.sleep(4000);
WebElement ele = Dr.findElement(By.xpath(".//*[#id='search']"));
ele.sendKeys("furniture");
ele.sendKeys(Keys.ENTER);
Thread.sleep(4000);
String result= Dr.findElement(By.xpath("/html/body/div[3]/div/div[4]/div[2]/div/div[1]/h1")).getText();
Assert.assertEquals("Furniture & Mattresses", result);
}
}
I used a very obvious element (Furniture & Mattresses) on the page to assert against its text
I am trying to do a sample program with selenium webdriver. I am using libraries from Selenium-java-2.53.1.
Here is my sample program
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
public class ScrollWebPage {
WebDriver driver;
String URL="https://www.gmail.com";
#BeforeClass
public void setUp(){
driver = new FirefoxDriver();
driver.get(URL);
driver.manage().window().maximize();
}
#Test(priority=1)
public void scrollingToBottom(){
((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)");
}
#AfterClass
public void tearDown(){
driver.quit();
}
}
The page is getting opened but it is not able to scroll down. seems an issue with executeScript()
Please help
.executeScript() expecting JavaScript string expression as first arguments while you are providing simply a String as Url which is not an JavaScript expression as exception says, You need to change :-
((JavascriptExecutor) driver).executeScript(URL, "window.scrollTo(0,document.body.scrollHeight)");
to
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
Note :- .executeScript() expect arguments like String arg0, Object... arg1 which means first arguments should be String but it should be JavaScript expression and second arguments should be Array of Object like Object[]
In your case no need to provide URL as arguments if you simply want to execute scrolling function.
Hope it will help you..:)
Simply Use as below to see the scroll working. Try in some other page because gmail don't have a much bigger page to feel the scroll.
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,2500)");
I'm looking to execute a Selenium 2 test against the Nightly browser (FireFox 64bit). It records just fine with the Selenium IDE (v1.8.1). And it also plays back just fine using using the IDE. I then export the code to TestNG format. By the way I've loaded up the Webdriver Backed plugin so it exports WebDriver code for the Selenium 2 version. The problem I'm having is that when I export the code to TestNG format (Java) and execute it, the asserts never find the text on the screen. It executes fine so its not that the code didn't convert. It just seems to be something with the asserts. If I play it from the IDE plugin it finds it the text and asserts just fine, however as soon as it executes in Java it fails all the assertions. Any ideas to what might be going on. My code is below. Thanks much!
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static junit.framework.Assert.*;
import com.thoughtworks.selenium.Selenium;
public class TestWithConfig {
WebDriver driver;
Selenium selenium;
#BeforeMethod
public void startSelenium() {
driver = new FirefoxDriver();
selenium = new WebDriverBackedSelenium(driver,
"http://en.wikipedia.org/wiki/Main_Page");
}
#AfterMethod
public void stopSelenium() {
driver.close();
}
#Test
public void testTest() {
selenium.setSpeed("600");
selenium.open("/wiki/Main_Page");
assertTrue("face not found",selenium.isTextPresent("face"));
selenium.click("link=Contents");
selenium.waitForPageToLoad("30000");
assertTrue("Below not found",selenium.isTextPresent("Below"));
selenium.click("link=Toolbox");
selenium.click("link=What links here");
selenium.waitForPageToLoad("30000");
assertTrue("Pages not found",selenium.isTextPresent("Pages that link to"));
selenium.click("link=exact:Talk:Wine");
selenium.waitForPageToLoad("30000");
assertTrue("Some not found",selenium.isTextPresent("Some"));
}
}
Since your using selenium 2 and webdriver, Assert's work a little different. I can see that you using the WebDriverBackedSelenium. However keep in mind. That's not selenium2. That is just a way to ease into selenium 2. I would use something like this.
WebElement tooltip = driver.findElement(By.xpath("the xpath of the element"));
assertNotNull("Name:","IP Address:",tooltip);
What I'm doing here is. i'm looking for a tooltip.inside that tooltip, there are two main labels that stay the same: Name and IP Address:. So I'm testing to see if those words exists or not in the tool tip. The output should be Name: IP Address:. That tells me the answer is true.