无法使用 selenium WebDriver 和 Javascript Executor 进行滚动

Not able to Scroll using selenium WebDriver and Javascript Executor

我正在尝试找出列表中的第 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);        
    } 

Url 网站 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>

有几件事需要注意:

  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++;
}

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

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  

终于找到答案了

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

}

我的测试Class

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