I am practising coding on Selenium Java.
I click on compose section and entered To,Subject and Body and trying to click on the dotted lines to select label as Social but unable to click on the section.
Please find the below code.
#Test
public void testSendEmail() throws Exception {
driver.get("https://mail.google.com/");
WebElement userElement = driver.findElement(By.id("identifierId"));
userElement.sendKeys(properties.getProperty("username"));
driver.findElement(By.id("identifierNext")).click();
Thread.sleep(1000);
WebElement passwordElement = driver.findElement(By.name("password"));
passwordElement.sendKeys(properties.getProperty("password"));
driver.findElement(By.id("passwordNext")).click();
Thread.sleep(1000);
WebElement composeElement = driver.findElement(By.xpath("//div[contains(text(),'Compose')]"));
composeElement.click();
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//textarea[#name='to']")));
//To Field
driver.findElement(By.name("to")).clear();
driver.findElement(By.name("to")).sendKeys(String.format("%s#gmail.com", properties.getProperty("username")));
//Subject Field
String emailSubject = properties.getProperty("email.subject");
driver.findElement(By.xpath("//input[#name='subjectbox']")).sendKeys(emailSubject);
//Body
String emailBody = properties.getProperty("email.body");
driver.findElement(By.xpath("//div[#class='Ar Au']//div")).sendKeys(emailBody);
//More options----Line where I am unable to click on the dotted lines to mark label as social.
driver.findElement(By.xpath("//div[#id=':q6']/div[2]")).click();
Thread.sleep(2000);
//Hover on Label
WebElement Label=driver.findElement(By.xpath("(//div[contains(text(),'Label')])[1]"));
WebElement Social=driver.findElement(By.xpath("//div[contains(text(),'Social')]"));
Actions a=new Actions(driver);
a.moveToElement(Label);
Thread.sleep(5000);
a.moveToElement(Social).click().build().perform();
Thread.sleep(5000);
//Click On Send
driver.findElement(By.xpath("//*[#role='button' and text()='Send']")).click();
It is very difficult to answer without seeing the html.
Instead of:
Actions a=new Actions(driver);
a.moveToElement(Label);
Thread.sleep(5000);
a.moveToElement(Social).click().build().perform();
Try this:
a.moveToElement(Label).build().perform();
waitUntil(ExpectedConditions.elementToBeVisible(Social)).click();
Related
I have performed drag and drop on unlayer web site and all positive combination I have used. but drap and drop not working.
We have tried following code with selenium 4.
#Test()
public void test() throws InterruptedException {
driver.get("https://dashboard.unlayer.com/create/blank?_gl=1*afclpx*_ga*NzU0NTMwNzU3LjE2NzYzMTAyOTk.*_ga_VMP9QH8KW8*MTY3NjMxMDI5OC4xLjEuMTY3NjMxMDMwOS41OC4wLjA.");
Thread.sleep(9000);
driver.switchTo().frame(0);
Thread.sleep(9000);
//driver.findElement(By.xpath("//div [text ()='Text']"));
WebElement source = driver.findElement(By.xpath("//div [text ()='Text']"));
//source.click();
Thread.sleep(9000);
WebElement target = driver.findElement(By.xpath("//div[text()='No content here. Drag content from right.']"));
System.out.println("target");
System.out.println("target= "+target.getText());
Thread.sleep(5000);
Actions a = new Actions(driver);
Thread.sleep(5000);
//a.clickAndHold(source).moveToElement(target).release().build().perform();
//a.clickAndHold(target);
//a.dragAndDrop(source, target);
//a.moveToElement(target);
a.click(source);
System.out.println("Click on="+"source");
Thread.sleep(5000);
a.release(target);
a.click(target);
System.out.println("Click on="+"target");
}
The desired element is within an <iframe>. So to perform Drag and Drop you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
You can use either of the following Locator Strategies: solutions:
Using dragAndDrop() method:
driver.get("https://dashboard.unlayer.com/create/blank?_gl=1afclpx_gaNzU0NTMwNzU3LjE2NzYzMTAyOTk._ga_VMP9QH8KW8*MTY3NjMxMDI5OC4xLjEuMTY3NjMxMDMwOS41OC4wLjA.");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-tid='banner-accept']"))).click();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='https://editor.unlayer.com']")));
Thread.sleep(10);
WebElement dragMe = driver.findElement(By.xpath("//div[#class='blockbuilder-content-tool' and #draggable='true']//div[text()='Text']"));
WebElement dropHere = driver.findElement(By.xpath("//div[text()='No content here. Drag content from right.']"));
Actions actions = new Actions(driver);
actions.dragAndDrop(dragMe, dropHere).build().perform();
Using clickAndHold().moveToElement().release() methods:
driver.get("https://dashboard.unlayer.com/create/blank?_gl=1afclpx_gaNzU0NTMwNzU3LjE2NzYzMTAyOTk._ga_VMP9QH8KW8*MTY3NjMxMDI5OC4xLjEuMTY3NjMxMDMwOS41OC4wLjA.");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-tid='banner-accept']"))).click();
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[src*='https://editor.unlayer.com']")));
Thread.sleep(10);
WebElement drag = driver.findElement(By.xpath("//div[#class='blockbuilder-content-tool' and #draggable='true']//div[text()='Text']"));
WebElement drop = driver.findElement(By.xpath("//div[text()='No content here. Drag content from right.']"));
Actions actions = new Actions(driver);
actions.clickAndHold(drag).moveToElement(drop).release(drop).build().perform();
I am working on Firstcry .com website for automation. After searched for Shoes int he search box, I need to scroll down to the bottom of the page to click "View All products" link. BUt scrolling is not happening.. what should be done... attached my code and screenshot for reference..
[public void f(String s) {
String ExpecTitle = "Kids Footwear - Buy Baby Booties, Boys Shoes, Girls Sandals Online India";
Actions builder = new Actions(Driver);
Driver.get("https://www.firstcry.com/");
String Viewall = "/html/body/div\[6\]/div\[2\]/div\[2\]/div\[2\]/div\[8\]/div\[2\]/span/a";
String MainTitle = Driver.getTitle();
System.out.println("Main title is " + MainTitle);
WebElement SearchBox = Driver.findElement(By.id("search_box"));
SearchBox.clear();
WebElement SearchBox2 = Driver.findElement(By.id("search_box"));
SearchBox2.sendKeys(s);
// SearchBox.sendKeys(Keys.ENTER);
//wait.until(ExpectedConditions.stalenessOf(Driver.findElement(By.cssSelector(".search-button"))));
Driver.findElement(By.cssSelector(".search-button")).click();
String ActTitle = Driver.getTitle();
System.out.println("The page title is " + ActTitle);
Driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
if(ActTitle.contains("Kids Footwear")){
System.out.println("Inside the if condition");
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
WebElement viewALL = Driver.findElement(By.xpath(Viewall));
// js.executeScript("arguments\[0\].scrollIntoView();", viewALL);
Driver.findElement(By.xpath(Viewall)).click();
System.out.println("View");
// WebElement viewAll = Driver.findElement(By.xpath("/html/body/div\[6\]/div\[2\]/div\[2\]/div\[2\]/div\[8\]/div\[2\]/span/a"));
// js.executeScript("arguments\[0\].scrollIntoView(true);", viewAll);
// wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a\[contains(text(),'View All Products')\]")));
// viewAll.click();
}
WebElement element = Driver.findElement(By.cssSelector(".sort-select-content"));
element.click();
builder.moveToElement(element).perform();
{
WebElement elem = Driver.findElement(By.linkText("Price"));
elem.click();
// builder.moveToElement(elem).perform();
}
//Driver.findElement(By.linkText("Price")).click();
}][1]
After searching for Shoes in the search box and selecting the first suggestion, to scroll down to the bottom of the page to click on the element with text as View All Products you need to induce WebDriverWait for the elementToBeClickable() and you can use the following xpath based Locator Strategies:
driver.get("https://www.firstcry.com/");
WebElement searchBox = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='search_box']")));
searchBox.clear();
searchBox.sendKeys("Shoes");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='searchlist']/ul/li/span"))).click();
((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='View All Products']"))));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='View All Products']"))).click();
Below code worked for me:
WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Driver.manage().window().maximize();
String url = "https://www.firstcry.com/";
Driver.get(url);
WebElement searchbox=Driver.findElement(By.id("search_box"));
searchbox.clear();
searchbox.sendKeys("shoes");
Driver.findElement(By.xpath("/html/body/div[1]/div[5]/div/div[2]/form/span")).click();
WebElement Element=Driver.findElement(By.partialLinkText("View All Products"));
JavascriptExecutor js = (JavascriptExecutor) Driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
Element.click();
// following code to scroll until element is visible and click that element
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement viewALL = Driver.findElement(By.xpath(Viewall));
js.executeScript("arguments[0].scrollIntoView();", viewALL);
viewALL.click();
}
}
I am having problem with locating WebElement using different locators. In the below html tag I tried locating the "write a review" WebElement with different locators like linkText,xpath,classname but still getting NoSuchElementException
-->url https://www.tripadvisor.in/-->search for Club Mahindra-->click on Club Mahindra-->click on write a review.
<a href="/UserReview-g641714-d1156207-Club_Mahindra_Madikeri_Coorg-
Madikeri_Kodagu_Coorg_Karnataka.html" target="_blank" class="ui_button
primary">Write a review</a>
Locators used
By.xpath("//*[#id="component_12"]/div/div[2]/div/div[2]/div/div[1]/a")
By.xpath("//a[#href='/UserReview-g641714-d1156207-
Club_Mahindra_Madikeri_Coorg-Madikeri_Kodagu_Coorg_Karnataka.html']")
By.className("ui_button primary")
By.linkText("Write a review")
I am really confused. What am I doing wrong?
I have tired to analyse and implement the same. Below are my findings:
-> Application waiting time is more as there are lots of dynamic loads applicable for the page.
-> Proper waits needs to be implemented
-> Check whether all the pages are getting opened in the same tab or clicking on each link is redirecting to new tabs, if so then we have to switch to that particular window.
-> Below code works like a pro for me.
driver.get("https://www.tripadvisor.in/");
WebDriverWait wait = new WebDriverWait(driver, 120);
WebElement ele1 =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Where to?']")));
ele1.click();
WebElement ele2= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#placeholder='Where to?']")));
ele2.sendKeys("club mahindra, india");
WebElement ele3= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Search for ')]")));
ele3.click();
WebElement ele4= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Club Mahindra Madikeri, Coorg')]")));
ele4.click(); //this click leads to a new tab
Set<String> winHandles = driver.getWindowHandles();
for(String str : winHandles) {
driver.switchTo().window(str);
}
System.out.println(driver.getTitle());
WebElement ele;
int i=1;
while(true) {
try {
ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[text()='Write a review']")));
break;
}catch(Exception e) {
System.out.print(i++);
}
}
System.out.println();
Actions action = new Actions(driver);
action.moveToElement(ele);
ele.click();
System.out.println("Clicked on the 'Write a review button'");
you can try
//a[contains(text(),'Write a review')]
I'm trying to submit an application on indeed using Selenium WebDriver.
I've entered jobType, and jobLocation, pressed search, opened up first result, and in new tab, switched focus, and pressed apply. Now, a pop up appears changing the previously 1 iframe on page to 2. I switch to second iframe and then try to send keys to input text field of "name" yet no input field exists
driver.get("https://www.indeed.com");
WebElement what = driver.findElement(By.id("text-input-what"));
what.sendKeys("Java Programmer");
WebElement where = driver.findElement(By.id("text-input-where"));
Thread.sleep(500);
for (int i = 0; i < 20; i++) {
where.sendKeys(Keys.BACK_SPACE);
}
where.sendKeys("Toronto, ON");
WebElement submit = driver.findElement(By.xpath("//button[#class='icl-Button icl-Button--primary icl-Button--md icl-WhatWhere-button']"));
submit.click();
WebElement thirdElement = driver.findElement(By.xpath("/html[1]/body[1]/table[2]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[2]/div[9]"));
thirdElement.click();
System.out.println(driver.getWindowHandle());
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
Thread.sleep(3000);
WebElement apply = driver.findElement(By.xpath("//button[#class='icl-Button icl-Button--branded icl-Button--md']//div[#class='jobsearch-IndeedApplyButton-contentWrapper'][contains(text(),'Apply Now')]"));
System.out.println(driver.findElements(By.tagName("iframe")).size());
apply.click();
driver.manage().window().maximize();
Thread.sleep(3000);
System.out.println(driver.getWindowHandle());
driver.switchTo().frame(1);
System.out.println(driver.getWindowHandle());
// Thread.sleep(3000);
System.out.println(driver.findElements(By.tagName("iframe")).size());
WebElement inputName = driver.findElement(By.xpath("//input[#id='input-applicant.name']"));
inputName.sendKeys("Adam Smith");
html code on ref page
.
Target indeed Page
The expected results are that i can input text to text field, and the actual is that it gives error message:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id='input-applicant.name']"}
(Session info: chrome=76.0.3809.87)
To achieve the inputName, you must switch the frame two times, please try this code to achieve what you mean :
driver.get("https://www.indeed.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("text-input-what")));
WebElement what = driver.findElement(By.id("text-input-what"));
what.sendKeys("Java Programmer");
WebElement where = driver.findElement(By.id("text-input-where"));
for (int i = 0; i < 20; i++) {
where.sendKeys(Keys.BACK_SPACE);
}
where.sendKeys("Jakarta");
WebElement submit = driver.findElement(By.xpath("//button[#class='icl-Button icl-Button--primary icl-Button--md icl-WhatWhere-button']"));
submit.click();
wait.until(ExpectedConditions.elementToBeClickable(By.className("title")));
WebElement thirdElement = driver.findElement(By.className("title"));
thirdElement.click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'Apply Now')]")));
WebElement apply = driver.findElement(By.xpath("//*[contains(text(),'Apply Now')]"));
apply.click();
driver.manage().window().maximize();
Thread.sleep(1000);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("[src^='https://apply.indeed.com/indeedapply/x'")));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("[src^='https://apply.indeed.com/indeedapply/resume']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='input-applicant.name']")));
WebElement inputName = driver.findElement(By.xpath("//input[#id='input-applicant.name']"));
inputName.sendKeys("Adam Smith");
Thread.sleep(2000);
I am trying to perform drag and drop with Selenium and Java and it is not working.. What can be the cause.. It doesn't give me any error but it is just not happening..
Here is my code.
public class ActionDragDrop {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement f=driver.findElement(By.xpath("//*[#id='iframeResult']"));
driver.switchTo().frame(f);
WebElement drag = driver.findElement(By.xpath("//*[#id='drag1']"));
WebElement drop = driver.findElement(By.xpath("//*[#id='div1']"));
Actions builder = new Actions(driver);
Actions dragAndDrop = builder.clickAndHold(drag);
builder.moveToElement(drop);
builder.release(drop);
builder.build();
dragAndDrop.perform();
}
}
You will to switch to iframe first in order to perform drag and drop event:
driver.switchTo().frame(0); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//your xpath"));
Actions act = new Actions(driver);
act.clickAndHold(from).build().perform();
Thread.sleep(4000);
driver.switchTo().defaultContent(); //Move outside to the frame.
driver.switchTo().frame(1); //Move inside to another frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement to = driver.findElement(By.id("guide_RIGHT_SAFETY_rect"));
act.clickAndHold(to).moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent(); //Move outside to another frame.
Note : Kindly use your xpath, id, classname etc, I have just copied an example. More or less idea should be same.
Try this below code.
As your from and to webelement is located inside the same iframe. First you need to switch inside the iframe.
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.manage().window().maximize();
driver.switchTo().frame("iframeResult"); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
WebElement from = driver.findElement(By.xpath("//img[#id='drag1']"));
WebElement to = driver.findElement(By.xpath("//div[#id='div1']"));
Actions act = new Actions(driver);
act.clickAndHold(from).perform();
Thread.sleep(4000);
act.clickAndHold().moveToElement(to).release(to).build().perform();
Thread.sleep(2000);
driver.switchTo().defaultContent(); //Move outside to the frame.
I have done many experiments and finally found the solution as below code in Python.
The failure at DragAndDrop is not caused by iframe.
Just separate every step and perform it.
# drag leftbox and drop on rightbox
actions = ActionChains(driver)
actions.click_and_hold(leftbox).perform()
sleep(4)
actions.move_to_element(rightbox).perform()
sleep(4)
actions.release(rightbox).perform()