Reputation: 47
I am trying to find out the 5th element in the list and click on it.
List of all the rooms stored :
@FindBy(xpath="//p[@class='css-6v9gpl-Text eczcs4p0']")
List<WebElement> placeListings;
Code:
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 for Website https://www.zoopla.co.uk/for-sale/property/london/?q=London&results_sort=newest_listings&search_source=home
Error Logs:
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>
Upvotes: 0
Views: 2140
Reputation: 47
Finally i got the answer
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();
}
}
my Testng Class
@Test
public void verifyHomeListings()
{
int countOfRooms = property.getpropertyListing();
Assert.assertEquals(25, countOfRooms);
property.clickon5thHouse();
}
Upvotes: 0
Reputation: 29362
Couple of things to take care :
There's a cookie button, I am selecting Accept all cookies
(If you do not interact with Cookie button) you would not be able to scroll down
.
Make use of JavascriptExecutor
and Actions class
Sample code :
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++;
}
Output :
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
===============================================
Update 2 :
for(WebElement e : allImgs) {
a.moveToElement(e).build().perform();
System.out.println(allPrices.get(i).getText());
if(i == 5) {
e.click();
break;
}
i++;
}
Upvotes: 1
Reputation: 1352
Just check with the below line of code, I hope the below code will help
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
Upvotes: 0