while automating the slider, The slider moves only when i placed my mouse over it in selenium - 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();

Related

Selenium java webdriver 3: moveToElement not working

Selenium java webdriver 3: moveToElement not working.
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Tried, adding click()
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
Not working. The mouse is not moved.
WebElement abcd = ..........
Actions actions = new Actions(driver);
actions.moveToElement(abcd).perform();
Above code will work, but please check with the chrome.exe version and the chrome version you are using it your machine. both should be compatible to one another. Check the compatibility with below link.
https://sites.google.com/a/chromium.org/chromedriver/
Skip the build() part, perform() does it underneath anyway.
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.linkText("host"));
actions.moveToElement(element).build().perform();
This will work. first check your "find element" method is write or wrong. Please post this step as well. otherwise your code is correct.
try to find element by xpath rather than link text. it worked for me.
WebElement element = driver.findElement(By.xpath("..."));
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Try below code
public static void mouse_movement(WebDriver driver, String xpathExpression) {
Actions act = new Actions(driver);
WebElement target = driver.findElement(By.xpath(xpathExpression));
act.moveToElement(target).build().perform();
}
if you need to click the element you could try javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath(xPath)));
I have solved this problem with:
const element = await driver.findElement(...)
await driver.executeScript("arguments[0].scrollIntoView(true);", element)
await driver.sleep(500);

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.

Selenium webdriver wait element and click

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();

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();

Select a text and perform a click action

I'd like to select some text and perform a click action - like in Winword where we click Bold after selecting some text...
I have to select the text and click on the <B> bold icon in the textarea.
Any idea on how to do this using Selenium/Webdriver?
In Java, The Advanced User Interactions API has your answer.
// the element containing the text
WebElement element = driver.findElement(By.id("text"));
// assuming driver is a well behaving WebDriver
Actions actions = new Actions(driver);
// and some variation of this:
actions.moveToElement(element, 10, 5)
.clickAndHold()
.moveByOffset(30, 0)
.release()
.perform();
I tried with Action builder and played with offset. It worked for me.
Actions action = new Actions(driver);
action.moveToElement(wblmt,3,3).click().keyDown(Keys.SHIFT).moveToElement(wblmt,200, 0).click().keyUp(Keys.SHIFT).build().perform();
I tried this way and it did not work. Here are the codes:
System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.vn");
driver.manage().window().maximize();
WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
Actions actions = new Actions(driver);
actions.moveToElement(text, 10, 5).clickAndHold().moveByOffset(30, 0).release().perform();
I switched to JavascriptExecutor and it worked:
System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.vn");
driver.manage().window().maximize();
WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'background: blue;');", text);