Use below simple test code but selenium always like to choose second google suggestion result as the search text:
For example:
I input "Selenium", google will give suggestion list like below:
Selenium
Selenium WebDriver
Then webdriver will always pick up "Selenium WebDriver". But I used webdriver to sendKeys as "Selenium".
Is it a bug to webdriver?
public class HelloWorld {
private WebDriver driver;
#Before
public void setUp() {
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(caps);
driver.get("http://www.google.com");
}
#After
public void tearDown() {
driver.quit();
}
#Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
//Thread.sleep(5000);
driver.findElement(By.name("btnK")).submit();
}
}
I'm pretty sure googles immediate results are bugging you here. Once you are halfway typing your query, Google will start showing you the results already and the "btnK" button will no longer be visible. Try this in stead:
#Test
public void testLitianyiNewsIsExisting() throws InterruptedException {
WebElement inputField = driver.findElement(By.name("q"));
inputField.sendKeys("selenium");
inputField.sendKeys(Keys.ENTER);
}
Related
I have tried all the methods including xpath but I am still not able to click on the Accept button on cookies popup on https://www.news.sky.com
Tried css selector, xpath, frame etc everything.
Here is my code:
public class Browser {
WebDriver driver;
public void browser_open() {
String projectPath = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectPath+"\\Drivers\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
public void navigate() throws InterruptedException {
driver.get("http://news.sky.com");
//Thread.sleep(5000);
driver.switchTo().frame("sp_message_iframe_368417");
driver.findElement(By.xpath("/html/body/div/div[3]/div[3]/button[1]")).click();
}
}
Please can someone help me on this?
I have already gone through a lot of posts on this and other forums but couldn't find an solution.
Thanks
I would suggest the xpath of //button[#title='Accept'] for what it's worth.
It's possible the switch to doesn't work because the element doesn't exist yet in the frame.
driver.switchTo().frame("sp_message_iframe_368417");
WebDriverWait wait = new WebDriverWait(driver, 10000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#title='Accept']")));
driver.findElement(By.xpath("//button[#title='Accept']")).click();
How to Post Status in Facebook using Selenium and Java?I have tried below code which doesn't work.Able to login but getting error no such element while posting status.After login i am getting notification popup allow or block,how to handle this popup also? Below the code i am using for testing this.
public class NewTest {
private WebDriver driver;
#Test
public void testEasy() throws InterruptedException {
driver.get("https://www.facebook.com/");
Thread.sleep(5000);
driver.findElement(By.id("email")).sendKeys("email");
driver.findElement(By.id("pass")).sendKeys("password" + Keys.ENTER);
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[#title=\"What's on your mind?\"]")).click();
driver.findElement(By.xpath("//textarea[#title=\"What's on your mind?\"]")).sendKeys("Hello World");
driver.findElement(By.xpath("//textarea[#title=\"What's on your mind?\"]")).sendKeys(Keys.ENTER);
}
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\admin\\Desktop\\Test\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
#AfterTest
public void afterTest() {
driver.quit();
}
}
Your XPath expression is not very correct, as far as I can see thte title of the relevant textarea looks like:
What's on your mind, user1984
so you need to amend your locator to use XPath contains() function like:
By.xpath("//textarea[contains(#title,\"What's on your mind\")]")
Using Thread.sleep is a performance anti-pattern, you should be using WebDriverWait instead. Example refactored code:
driver.get("https://www.facebook.com/");
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("email");
wait.until(ExpectedConditions.elementToBeClickable(By.id("pass"))).sendKeys("password" + Keys.ENTER);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(#title,\"What's on your mind\")]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(#title,\"What's on your mind\")]"))).sendKeys("Hello World");
I am trying to test an E-Commerce website using Selenium webdriver. The problem in the test is that whenever I try to add stuff in the cart it just pops a news letter window which I tried handling using alert but I cannot.
Can someone please help me. I am attaching a screenshot below along with the code.
public class Ui {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='search_txt']")).sendKeys("K-6626-6U ");
Actions enter = new Actions(driver);
enter.moveToElement(driver.findElement(By.xpath(".//*[#id='site-search']/div/button"))).click().build().perform();
}
}
First thing is - Its not an alert it a window popup So you need to locate the close button and then click
Use below code for the same :
public class Ui
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","C:/New folder/geckodriver.exe");
//First Iam going to initialize the webdriver by using Firefox driver//
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOf( driver.findElement(By.xpath("//button[#class='close external-close']")))).click();
}
}
Here you have to use ExplicitWait until popup get visible and then have to perform click. If you won't use the wait then it will throw ElementNotVisibleException.
wait for some time and click on escape
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.build.com/");
driver.manage().window().maximize();
//give own time
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions enter = new Actions(driver);
enter.sendKeys(Keys.ESCAPE).perform();
}
I'm not clear why I am getting 3 chrome browsers opening for the following example. I have an #Before (cucumber version) annotation to simply setup a chrome webdriver instance before the scenario runs. As far as I can see, it should open one browser, run scenario (step defs) then close using the #After cucumber hook. What happens is 2 windows open before a third and final window actually executes the steps:
Scenario:
Given I am on the holidays homepage
When I select the departure location "LON"
And I select the destination location "PAR"
And I submit a search
Then search results are displayed
Step Def:
public class FindAHolidayStepDefs {
private WebDriver driver;
#Before
public void setup() {
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir") + "\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
#Given("^I am on the Holidays homepage$")
public void IAmOnTheThomasCookHomepage() {
driver.get("http://uat7.co-operativetravel.co.uk/");
driver.manage().window().maximize();
String pageTitle = driver.getTitle();
assertEquals("the wrong page title was displayed !", "Holidays - welcome", pageTitle);
}
#When("^I select the departure location \"([^\"]*)\"$")
public void ISelectTheDepartureLocation(String departureAirport) {
WebElement dropDownContainer = driver.findElement(By.xpath("(//div[#class=\"custom-select departurePoint airportSelect\"])[1]"));
dropDownContainer.click();
selectOption(departureAirport);
}
#When("^I select the destination location \"([^\"]*)\"$")
public void ISelectTheDestinationLocation(String destinationAirport) {
WebElement destinationField = driver.findElement(By.xpath(("(//div[#class=\"searchFormCol destinationAirport\"]/div[#class=\"combinedInput searchFormInput\"]/span/input)[1]")));
destinationField.sendKeys(destinationAirport);
selectOption("(" + destinationAirport + ")");
}
#When("^I submit a search$")public void iSubmitASearch() throws Throwable {
WebElement submitButton = driver.findElement(By.xpath("(.//*[#type='submit'])[1]"));
submitButton.click();
}
#Then("^search results are displayed$")
public void searchResultsAreDisplayed() throws Throwable {
waitForIsDisplayed(By.xpath(".//*[#id='container']/div/div[3]/div/div[1]/div/h3"), 30);
assertThat(checkPageTitle(), equalTo("Package Results"));
}
#After
public void tearDown() {
driver.quit();
}
}
When I step through the code in Intellij, the following method is called:
private void runHooks(List<HookDefinition> hooks, Reporter reporter, Set<Tag> tags, boolean isBefore)
and Intellij reports that hooks paramter = 3 at this point.
hooks: size=3 reporter: "null" tags: size = 0 isBefore: true
Do you have more then one scenario in your feature? -> The #Before method will be executed before each scenario.
Do you have a different stepdef class with #Before annotated method that opens chrome? -> All #Before methods will be called before a scenario is executed.
I am new to Selenium and trying to use Actions class to mouseover on the Profile icon available on linked in site to open the menu that appears on Mouseover of profile image.
Below is my code and when it reaches on to those lines the error comes : Unable to locate element..
This is happening with all the icons available on Linked on top bar ( messages / Flag icon etc.
Code :
public class LinkedIn {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() throws Exception {
String baseUrl = "http://www.linkedin.com/";
driver.get(baseUrl);
}
#Test
public void login() throws InterruptedException
{
WebElement login = driver.findElement(By.id("login-email"));
login.sendKeys("*****#gmail.com");
WebElement pwd = driver.findElement(By.id("login-password"));
pwd.sendKeys("*****");
WebElement in = driver.findElement(By.name("submit"));
in.click();
Thread.sleep(10000);
}
#Test
public void profile() {
// here it gives error to me : Unable to locate element
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='img-defer-id-1-25469']"));
action.moveToElement(profile).build().perform();
driver.quit();
}
}
It seems you have used incorrect xpath , Kindly check below example to mouse hover on Message button :
Thread.sleep(5000);
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='account-nav']/ul/li[1]"));
action.moveToElement(profile).build().perform();
Correct Xpaths are :
For Message Icon : "//*[#id='account-nav']/ul/li[1]"
For Connection Icon : //*[#id='dropdowntest']
Above code I just tested and working fine so will work for you.