【问题标题】:Not able to Scroll using selenium WebDriver and Javascript Executor无法使用 selenium WebDriver 和 Javascript Executor 滚动
【发布时间】:2021-09-02 04:50:53
【问题描述】:

我正在尝试找出列表中的第 5 个元素并单击它。
所有已存储房间的列表:

@FindBy(xpath="//p[@class='css-6v9gpl-Text eczcs4p0']")  
    List<WebElement> placeListings;   

代码:

public void clickon5thHouse()
    {
    Web4 = placeListings.get(4);   // **This is a list of all the webelements in <div> tag | I am picking 4th element from the list and trying to click on it** 
        
        int x = Web4.getLocation().getX(); 
        int y = Web4.getLocation().getY();

        //scroll to x y 
        JavascriptExecutor js = (JavascriptExecutor) driver;
       WebDriverWait wait;
      wait = new WebDriverWait(driver,40);
        //((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", Web4);
      //((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.documentElement.scrollHeight);");
      js.executeScript("window.scrollBy(" +x +", " +y +")");        
      wait.until(ExpectedConditions.elementToBeClickable(Web4));
    Web4.click();
        //((JavascriptExecutor) driver).executeScript("arguments[0].click();", Web4);        
    } 

网站网址 https://www.zoopla.co.uk/for-sale/property/london/?q=London&results_sort=newest_listings&search_source=home

  1. 使用 Chrome 浏览器。
  2. 尝试使用所有可能的滚动方法,您可以查看注释代码。
  3. 对于所有滚动命令,它只是滚动到网站中的同一点,我不确定我是否遗漏了任何内容。 Scrollbar is reaching till this point for all the scroll commands

错误日志:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <p size="6" class="css-6v9gpl-Text eczcs4p0">...</p> is not clickable at point (845, 13).    
Other element would receive the click: <a data-testid="listing-details-link" href="/for-sale/details/58485081/" class="e2uk8e4 css-15tydk8-StyledLink-Link-FullCardLink e33dvwd0">...</a>

【问题讨论】:

    标签: javascript java selenium webdriver


    【解决方案1】:

    需要注意的几件事:

    1. 有一个cookie按钮,我选择Accept all cookies(如果你不与Cookie按钮互动)你将无法scroll down

    2. 利用JavascriptExecutorActions class

    示例代码:

    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://www.zoopla.co.uk/for-sale/property/london/?q=London&results_sort=newest_listings&search_source=home");
    WebDriverWait wait = new WebDriverWait(driver, 10);     wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[class$='ui-cookie-accept-all-medium-large']"))).click();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("window.scrollBy(0, 250)");
    Actions a = new Actions(driver);
    List<WebElement> allImgs = driver.findElements(By.cssSelector("a[data-testid='listing-details-image-link'] img"));
    ArrayList<WebElement> allPrices = (ArrayList<WebElement>) driver.findElements(By.cssSelector("div[class*='CardHeader'] p"));
    System.out.println(allImgs.size() + "and " + allPrices.size());
    int i = 0;
    for(WebElement e : allImgs) {
        a.moveToElement(e).build().perform();
        System.out.println(allPrices.get(i).getText());
        i++;
    } 
    

    输出:

    25and 32
    £2,140,000
    Offers in region of
    £525,000
    £650,000
    £480,000
    £270,000
    Guide price
    £750,000
    £1,260,000
    £475,000
    £695,000
    £450,000
    £795,000
    £335,000
    £849,950
    Offers over
    £650,000
    Offers over
    £725,000
    Guide price
    £430,000
    Guide price
    £300,000
    £520,000
    £1,500,000
    PASSED: testSO
    
    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    

    更新 2:

    for(WebElement e : allImgs) {
        a.moveToElement(e).build().perform();
        System.out.println(allPrices.get(i).getText());
            if(i == 5) {
                e.click();
                break;
            }
        i++;
    }
    

    【讨论】:

    • 嗨@cruisepandey,我尝试使用上述所有代码,但仍然收到以下错误org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element &lt;p size="6" class="css-6v9gpl-Text eczcs4p0"&gt;...&lt;/p&gt; is not clickable at point (526, 551). Other element would receive the click: &lt;a data-testid="listing-details-link" href="/for-sale/details/58064451/" class="e2uk8e4 css-15tydk8-StyledLink-Link-FullCardLink e33dvwd0"&gt;...&lt;/a&gt; (Session info: chrome=91.0.4472.101)
    • 错误是什么?你用的是哪一个?
    • 您是否全屏启动驱动程序?它是无头的吗?
    • 对于所有滚动命令,它只是滚动到网站中的同一点
    • 也许我可以看看.. 但给我时间
    【解决方案2】:

    只需检查下面的代码行,我希望下面的代码会有所帮助

    driver.get(" https://www.zoopla.co.uk/for-sale/property/london/? 
    q=London&results_sort=newest_listings&search_source=home");
        
    WebElement element = driver.findElement(By.xpath("(((//*[@data-testid='search-result'])[5])/div/div[2]/div[2]//following-sibling::a)"));
    
    ((JavascriptExecutor)driver).executeScript
    ("arguments[0].scrollIntoView(true);", element);
        
    element.click();
     
    edit it as per your requirement  
    

    【讨论】:

    • 嗨 Manish,感谢您花时间回答,我也更新了我的问题,我必须点击列表中的第五个元素。 WebElement 是动态的,stackoverflow 的新手,请忽略愚蠢的错误
    • @Jyoti 您可以检查答案中的 XPath,这可能会对您有所帮助
    【解决方案3】:

    终于找到答案了

    import com.zoopla.qa.base.Base;
    
    public class propertyListing extends Base {
    
        static int Web4;
        @FindBy(xpath="//p[@class='css-6v9gpl-Text eczcs4p0']")
        List<WebElement> placeListings;
        List<Integer> sortedPlaceListings = new ArrayList<>();
        
        public propertyListing() throws IOException {
            PageFactory.initElements(driver, this);
        }
        
        public int getpropertyListing()
        {    
            for(WebElement e: placeListings)
            {   
                String textList = e.getText().substring(1);
                String changedText = textList.replaceAll(",", "");
                if(changedText != "POA")
                {
                sortedPlaceListings.add(Integer.parseInt(changedText));
                }
            }
            Collections.sort(sortedPlaceListings , Collections.reverseOrder()); 
            for(Integer s: sortedPlaceListings)
                System.out.println(s);
            return sortedPlaceListings.size();
        }
        public void clickon5thHouse()
        {
            Web4 = sortedPlaceListings.get(4);
             NumberFormat formatter=NumberFormat.getCurrencyInstance(Locale.UK);  
             formatter.setMaximumFractionDigits(0);
             String currency=formatter.format(Web4); 
            WebElement element = driver.findElement(By.xpath("//p[contains(text(),'"+currency+"')]//parent::div/parent::div/following-sibling::a"));
           WebDriverWait wait;
          wait = new WebDriverWait(driver,40);
            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
          wait.until(ExpectedConditions.elementToBeClickable(element));
        element.click();     
        }   
    
    }
    

    我的测试类

        @Test
        public void verifyHomeListings()
        {
            int countOfRooms = property.getpropertyListing();
            Assert.assertEquals(25, countOfRooms);
            property.clickon5thHouse();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 2017-04-26
      • 2015-02-28
      • 2013-04-14
      相关资源
      最近更新 更多