在Selenium Java中滚动到一个元素失败了。

huangapple go评论67阅读模式
英文:

scrolling to an element failed in Selenium Java

问题

I am working on Firstcry .com website for automation. After searching for Shoes in the 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);
    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));
        Driver.findElement(By.xpath(Viewall)).click();
        System.out.println("View");
    }

    WebElement element = Driver.findElement(By.cssSelector(".sort-select-content"));
    element.click();
    builder.moveToElement(element).perform();

    {
        WebElement elem = Driver.findElement(By.linkText("Price"));
        elem.click();
    }
}

在Selenium Java中滚动到一个元素失败了。

英文:

在Selenium 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]

答案1

得分: 1

在搜索框中搜索鞋子并选择第一个建议后,滚动到页面底部点击具有文本“查看所有产品”的元素需要使用WebDriverWait来等待elementToBeClickable(),您可以使用以下基于[xpath]的定位策略

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()='查看所有产品']"))));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='查看所有产品']"))).click();

请注意,这是一个Selenium WebDriver的代码示例,用于在网页上执行操作。

英文:

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 [tag: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();

答案2

得分: 0

以下是代码的翻译部分:

// 以下代码将滚动直到元素可见并点击该元素

JavascriptExecutor js = (JavascriptExecutor) driver;

WebElement viewALL = Driver.findElement(By.xpath(Viewall));

js.executeScript("arguments[0].scrollIntoView();", viewALL);
viewALL.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();
}

}

答案3

得分: 0

The code you provided in English is as follows:

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

Here is the code translated into Chinese:

WebDriver 驱动程序 = new ChromeDriver();
驱动程序.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
驱动程序.manage().window().maximize();
String 网址 = "https://www.firstcry.com/";
驱动程序.get(网址);
WebElement 搜索框 = 驱动程序.findElement(By.id("search_box"));
搜索框.clear();
搜索框.sendKeys("鞋子");
驱动程序.findElement(By.xpath("/html/body/div[1]/div[5]/div/div[2]/form/span")).click();
WebElement 元素 = 驱动程序.findElement(By.partialLinkText("查看所有产品"));
JavascriptExecutor js = (JavascriptExecutor) 驱动程序;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
元素.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();

huangapple
  • 本文由 发表于 2020年8月1日 03:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63198258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定