Selenium webdriver wait element and click - selenium

InternetExplorerDriver driver = new InternetExplorerDriver();
driver.get("http://www.wiki-doctor.com");
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.id("btnSearch")));
element.click();
it waits for the element btnSearch to be displayed and click on it, however, it doesn' t seems to do anything, do you have any idea why it happens ?
Thanks

This adds United States as a locale, and then waits until a picture of the doctor is displayed.
driver.get("http://www.wiki-doctor.com");
//Enter united states into field
driver.findElement(By.id("field-findDoctor-town")).sendKeys("United States");
WebElement element = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.id("btnSearch")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
WebDriverWait wait = new WebDriverWait(driver,10);
//Wait for picture of doctor
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#dvparcticianlist > div.row > div > div.listing-row > div.doc-photo-container")));
System.out.println("Search Successful");
}

Once the page is loaded, first we need to wait for the intended WebElement i.e. the first Search Box to be clickable. Then we will send some text to both the Search Boxes and then invoke click() method on the Search Button as follows :
System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.wiki-doctor.com");
WebElement locality = (new WebDriverWait(driver, 5))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='field-findDoctor-town']")));
locality.sendKeys("Pune");
driver.findElement(By.xpath("//input[#id='speciality']")).sendKeys("Doctor");
driver.findElement(By.xpath("//button[#id='btnSearch']")).click();

WebDriverWait wait =new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
driver.findElement(By.xpath("xpath")).click();

Related

How to handle the popup within the eCommerce website https://www.firstcry.com using Selenium and Java

`
WebDriver Driver = null;
WebDriverWait wait = null;
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches",Arrays.asList("disable-popup-blocking"));
options.addArguments("--disable-popup-blocking");
Driver = new ChromeDriver(options);
Driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
wait = new WebDriverWait(Driver, 25);
}
#Test(dataProvider = "dp")
public void Test( String s) {
String SearchWord = s;
String url = "https://www.firstcry.com/";
Driver.get(url);
wait.until(ExpectedConditions.alertIsPresent());
By popupdeny1 = By.xpath("//*[#id=\"deny\"]");
By searchbox = By.id("search_box");
By sortSelect = By.xpath("/html/body/div[5]/div[2]/div/div[2]/div[1]/div[2]/div[1]/div");
By descendingPrice = By.xpath("/html/body/div[5]/div[2]/div/div[2]/div[1]/div[2]/div[1]/ul/li[4]/a");
if(Driver.findElement(popupdeny1).isDisplayed())
Driver.findElement(popupdeny1).click();`
I am automating firstcry webpage for a study purpose. There are few popups in this url, which i need to handle before searching for a product and sort the items based on price
https://www.firstcry.com/
I even added Chromeoptions in browser setting, but still the popups are coming...here is the code part.
I tried to click the deny button in my code, but ended with error saying cant find the element.
Any help is appreciated
To click() the element with text as Allow within the url https://www.firstcry.com/, as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using cssSelector:
driver.get("https://www.firstcry.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='webpush-onsite']")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#allow"))).click();
Using xpath:
driver.get("https://www.firstcry.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#title='webpush-onsite']")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#id='allow']"))).click();
Browser Snapshot:

The radio button is not getting selected with this HTML code for that. How to do that?

<input id="radio2" name="radioGroup" type="radio">
<label class="flRight" for="radio2">
::before
"Senior Citizen"
::after
</label>
WebElement senior = driver.findElement(By.id("radio2"));
senior.click();
Now the problem is the code is not able to click the desired element.
You need to induce WebdriverWait And to click on the radio button use JavaScript Executor since neither webdriver click nor action class is working
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement item=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#class='flRight' and #for='radio2']")));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", item);
Try with this:
WebElement senior = driver.findElement(By.xpath(".//div[#class='radioBtn']/label[2]"));
WebElement close = driver.findElement(By.xpath(".//div[#id='nvpush_cross']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(close));
close.click();
senior.click();
Code didnt worked before because there is popup you need to close it.
Use WebDriverWait:
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.id("radio2")));
senior.click();
After seeing the WebSite it looks like the input is not the element you want to click rather the label...
So just change the senior var to:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='RadioButton']//label[#for='radio2']")));
The best way to automate human actions in the case where the element is not clickable dew to other elements covering them is to use Actions:
WebElement senior = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='RadioButton']//label[#for='radio2']")));
Actions actions = new Actions(driver);
actions.moveToElement(senior).click().build().perform();
(using JavascriptExecutor and executeScript doesn't realy click... it just invokes the method that can be good but not for testing...)
As a last resort use JavascriptExecutor:
JavascriptExecutor jse= (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", senior);
Please try below solution :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#class='flRight' and #for='radio2']")));
Actions action=new Actions(driver);
action.moveToElement(element).click().perform();

wait is not working but thread.sleep is working, also not throwing exception in selenium

//Thread.sleep(300);
Actions action = new Actions(driver);
WebDriverWait wait3 = new WebDriverWait(driver, 60);
wait3.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[#id='toggleNav']/li[2]/a")));
WebElement we = driver.findElement(By.xpath(".//*[#id='toggleNav']/li[2]/a"));
action.moveToElement(we).perform();
WebDriverWait wait4 = new WebDriverWait(driver, 60);
wait4.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='toggleNav']/li[2]/ul/li[3]/a")));
WebElement we1 = driver.findElement(By.xpath("//*[#id='toggleNav']/li[2]/ul/li[3]/a"));
action.moveToElement(we1).click().build().perform();
With thread.sleep it works fine, but when used wait its not performing actions and also not throwing any element not found exceptions...
FYI used Java script too, in this case as well its not working when thread.sleep is commented
//Thread.sleep(300);
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath(".//*[#id='toggleNav']/li[2]/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", we);
// action.moveToElement(we).perform();
WebElement we1 = driver.findElement(By.xpath("//*[#id='toggleNav']/li[2]/ul/li[3]/a"));
JavascriptExecutor executor1 = (JavascriptExecutor)driver;
executor1.executeScript("arguments[0].click();", we1);
//action.moveToElement(we1).click().build().perform();
Instead of using
wait.until(ExpectedConditions.presenceOfElementLocated(By....)
use
wait.until(ExpectedConditions.elementToBeClickable(By....)
This way you are waiting for the found element to be clickable before proceeding.

while automating the slider, The slider moves only when i placed my mouse over it in selenium

http://automationpractice.com/index.php?id_category=5&controller=category#/
This is my code,whats wrong with it?
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all.ui-state-hover");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,-40,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();
Where's actions taking under consideration the driver.
Try to enter the following code,
Actions action=new Actions(WebDriver);
You have used a wrong cssSelector. I have modified your code and it should work now.
Here is the java code
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?id_category=5&controller=category#/");
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
//modified the css selector
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,70,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();

Xpath is not working on MouseOver(CatMenu) in Selenium

There is problem about selenium for all web driver which cannot open mouseover menu.Altough assing to Xpath of element web driver cannot open Mouse Over (CatMenu) and log is that "Must provide a location for a move action".
i want to go n11.com web adress and over on Kitap, Müzik, Film, Oyun and click Kitap but its not working.
Thank you
#Test
public void startWebDriver(){
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.n11.com");
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath(".//*[#id='contentMain']/div/nav/ul/li[8]/a"))).perform();
}
Use following code to achieve the same -
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
driver =new ChromeDriver();
driver.get("http://www.n11.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement menu = driver.findElement(By.xpath("//li[#class='catMenuItem']/a[#title='Kitap, Müzik, Film, Oyun']"));
WebElement submenu = driver.findElement(By.xpath("//li[#class='subCatMenuItem']/a[#title='Kitap']"));
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(submenu).click().build().perform();
Use some Implicit Wait to avoid timeout exceptions to find your web element
Use more specific xpath to find your web element.
In your case first you need to hover on Kitap, Müzik, Film, Oyun menu and then have to perform click on Kitap submenu
You can try to use ExplicitWait with more specific XPath:
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//a[#title='Kitap, Müzik, Film, Oyun']")[2])));
// WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Kitap, Müzik, Film, Oyun")));
Actions act = new Actions(driver);
act.moveToElement(element).perform();