scrolling to an element failed in Selenium Java - java

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

Related

Drag and drop not working in unlayer web site

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

Unable to click a button inside iframe which itself is inside shadow root in Selenium WebDriver using Java

driver.get("https://thesportstak.com/");
Thread.sleep(4000);
WebElement scrollStory = driver.findElement(By.xpath("//div[#id='story3']"));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
wait.until(ExpectedConditions.visibilityOf(scrollStory));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", scrollStory);
List<WebElement> homeStories = driver.findElements(By.xpath("//div[#class=\"mainDiv \"]"));
List<WebElement> homeStoriesptag = driver.findElements(By.xpath("//div[#class=\"mainDiv \"]//p"));
for (WebElement homeStoryp : homeStoriesptag) {
System.out.println("getting home story p tag " + homeStoryp);
System.out.println("Text of home story " + homeStoryp.getText());
}
WebElement firstStory =homeStories.get(3);
firstStory.click();
Thread.sleep(3000);
WebElement shadow = driver.findElement(By
.cssSelector("div[class=\"i-amphtml-fill-content i-amphtml-story-player-shadow-root-intermediary\"]"));
// This Element is inside single shadow DOM.
SearchContext shadowroot = shadow.getShadowRoot();
Thread.sleep(1000);
WebElement iframe = shadowroot.findElement(By.cssSelector(" div:nth-child(1) > iframe:nth-child(3)"));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(iframe));
List<WebElement> please = driver.findElements(By.xpath("//div[#class=\"letterbox\"]//p"));
System.out.println(please);
WebElement nextbutton =driver.findElement(By.cssSelector("button[aria-label='Next page']"));
for(WebElement letterstory : please) {
wait.until(ExpectedConditions.attributeToBeNotEmpty(letterstory,"class"));
wait.until(ExpectedConditions.elementToBeClickable(nextbutton));
***nextbutton.click();*** ---->>>
I am trying to click the above button. Next button.click. I have tried both xpath and css selector to find the button.
Also, I can only fetch the text from the first story above, the rest elemenets are present, but no text can be fetched from that. the p tag and the texts are present in the DOM and can be seen in the first story itself. last line.
Kindly help regarding the same.
System.out.println("stories element "+ letterstory);
***System.out.println("stories inside "+letterstory.getText());***
}

Problems to click in a JQUERY element using Selenium Webdriver

I'm trying to click in some JQUERY elements from a very known website to practice Selenium (http://the-internet.herokuapp.com/jqueryui/menu).
I figured out how to navigate into the menu (not sure if my code is a good solution), however am not being able to click in each last submenu option (PDF, CSV, Excel)
I'm trying something like below:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
String jQuerySelector = "$('a#ui-id-6.ui-corner-all')";
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").get(0);");
//click() also did not work
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").click();");
Your JavaScript function of click is wrong.
Use below javascript syntax
executor.executeScript("arguments[0].click();", WebElement);
Below code works for me:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-id-4")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("ui-id-4")));
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
WebElement webElement2= driver.findElement(By.cssSelector("a#ui-id-6.ui-corner-all")); // #ui-id-6 is for pdf, #ui-id-7 csv so on
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webElement2);

Can't click anything, despite switching frames, on frame popup on Indeed when using selenium

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

How to click more options in Compose sections using Selenium?

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

Categories

Resources