Select a text and perform a click action - selenium

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

Related

Drag and drop not working - Selenium Webdriver

I am trying to drag an element into another element using Selenium WebDriver but it's not working.
We tried with different solutions as mentioned below :
Application was built on Anjular4
WebElement From = (driver.findElement(By.xpath("")));
WebElement To = (driver.findElement(By.xpath("//*[#id='avpContainer']"));
Actions builder = new Actions(driver);
builder.clickAndHold(From).moveToElement(To).click(To).release().build().perform();
WebElement From = (driver.findElement(By.xpath("")));
WebElement To = (driver.findElement(By.xpath("//*[#id='avpContainer']"));
Actions builder = new Actions(driver);
builder.clickAndHold(From).release(To).build().perform();
WebElement From = (driver.findElement(By.xpath("")));
WebElement To = (driver.findElement(By.xpath("//*[#id='avpContainer']"));
Actions builder = new Actions(driver);
builder.dragAndDrop(From, To).build().perform();
//Setup robot
Robot robot = new Robot();
robot.setAutoDelay(50);
//Maximized browser:
robot.keyPress(KeyEvent.VK_F11);
Thread.sleep(2000);
WebElement dragElement = d.findElement('drag element');
Actions builder = new Actions(d);
builder.dragAndDropBy(dragElement,x, y).build().perform();
Can anyone help to resolve this issue.
WebElement From = driver.findElement(By.xpath(""));
WebElement To = driver.findElement(By.xpath(""));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(From)
.moveToElement(To)
.release(To)
.build();
dragAndDrop.perform();
Drag and Drop functionality won't work, if application is built on HTML5.
So, we can make this work with the support of draganddrop.js

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

How to select the submenu of the flyout using selenium webdriver

I want to select the submenu of the flyout using selenium webdriver.
Also how can I simulate on hover on menu items using selenium.
Please guide or provide some tutorial for the same.
Thanks
Check the actions class, something like this you need:
WebElement element = driver.findElement(By.Id("id"));
Actions builder = new Actions(driver);
Actions hover = builder.moveToElement(element);
hover.build().perform();
String New_select_Just_Landed = "//*[#id='nav1']/a";
WebElement Ba = driver.findElement(By.xpath(New_select_Just_Landed));
WebElement subBa = driver.findElement(By.xpath("//*[#id='nav1']/section/ul/li/a"));
Actions action = new Actions(driver);
action.moveToElement(Ba).perform();
Thread.sleep(2000); System.out.println("Mouse hoover succeed");
action.moveToElement(subBa).perform();
subBa.click();
System.out.println("Click Just Landed! succeed");
Thread.sleep(5000L);

Unable to click a value from a sub list of the main list using mouse over action of selenium webdriver

I am trying to automate a scenario where I can select a value from a sub-menu present under main menu.
Below is the application url:
http://www.jetairways.com/EN/IN/Home.aspx
Its a mouser over functionality and tried to automate the Tab (Plan your travel-> Flights -> Book Online) with the below code but not working:
WebElement we = driver.findElement(By.xpath(".//*[#id='PlanYourTravel']/span/b"));
WebElement we1 = driver.findElement(By.xpath(".//*[#id='lnkThirdLevel58']"));
WebElement we2 = driver.findElement(By.xpath(".//*[#id='ddsubSubmenu58']/li[1]/a"));
Actions builder = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
builder.moveToElement(we).perform();
Thread.sleep(10000);
wait.until(ExpectedConditions.visibilityOf(we1));
builder.moveToElement(we1).perform();
wait.until(ExpectedConditions.visibilityOf(we2));
builder.moveToElement(we2).click().perform();
I am getting the element not visible issue. Quick help on this would be much appreciated.
I have tried with below code to click on "Book Online" and its working fine
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("http://www.jetairways.com/EN/IN/Home.aspx");
Actions actObj = new Actions(driver);
//actObj.moveToElement(driver.findElement(By.xpath("//*[text()='Plan Your Travel']"))
actObj.moveToElement(driver.findElement(By.xpath("//*[#id='PlanYourTravel']/span/b"))).perform();
actObj.moveToElement(driver.findElement(By.xpath("//li[#title='Flights']/a"))).perform();
driver.findElement(By.xpath("//a[#title='Book Online']")).click();