I am trying to view all the houses that this site has to offer. For that I used while loop to keep clicking show_more_listings button
to show more and more results. Although I achieved that I am unable to end printing of text inside except statement in VS code terminal. I tried using break in both try and except block but doing so will not allow me to parse the whole list, meaning the button is clicked only one time and then breaks or stops. I also tried to find conditions to stop loop such as NoSuchElementException
as well as nesting while in try block but none of that worked. Here is the code:
from logging import exception
from typing import Text
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
import pandas as pd
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import csv
from selenium import webdriver
PATH = "C:/ProgramData/Anaconda3/scripts/chromedriver.exe" #always keeps chromedriver.exe inside scripts to save hours of debugging
driver =webdriver.Chrome(PATH) #preety important part
driver.get("https://www.gharghaderi.com/house-for-sale/")
driver.implicitly_wait(10)
while(True):
try:
show_more_listings = driver.find_element_by_xpath('//span[@class="show_more"]/button')
show_more_listings.click()
except:
print("you have reached end of the list no more houses to be shown") #this keeps printing infinitely in vs code terminal
#break, is not used here cause it prevents furthur clicking of show_more_listings to view more houses
The problem with your code is the While Loop
, it is an infinite loop.
As your passing while(True):
it will keep executing and the try-except will not allow it to break as it will handle the exception situation.
To fix this you need to set a certain condition to break it.
Code:
def isDisplayed():
try:
driver.find_element_by_xpath('//span[@class="show_more"]/button')
except NoSuchElementException:
return False
return True
while
loop when the show_more
button is visible else terminating the loop.Code:
while(isDisplayed()):
try:
show_more_listings = driver.find_element_by_xpath('//span[@class="show_more"]/button')
show_more_listings.click()
time.sleep(3)
except:
print("you have reached end of the list no more houses to be shown")
Note: I have to use the time.sleep(3)
to allow next load_more
button to load. Please use explicit wait etc.
Output: No error