Issue
I'm trying to click load more button several times using selenium, however I cannot click load more button (it is even not a button...)
When I try to click it, it shows error element click intercepted: Element is not clickable at point
even after I explicitly code wait.until(EC.element_to_be_clickable
)
I wonder what can be wrong with my code. The code I'm using is
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
url = "https://www.architectural-review.com/buildings/house"
options = webdriver.ChromeOptions()
driver = webdriver.Chrome()
driver.get(url)
# accept cookies
wait0 = WebDriverWait(driver, 10)
cookie_button = wait0.until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[1]/div/div[6]/button[1]')))
cookie_button.click()
print("loading more projects ...")
count=5
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[2]/div/div[3]/section[5]/div/div[5]/a')))
print(element)
print("now element is clickable")
while count>1:
element.click();
count-=1
print(count)
and the HTML looks like (it's not a button, not sure whether this matters)
<div class="view-more">
<a href="#" class="cpb-bottom-more-news-link dynamic-loader" data-term-id="869" data-block-id="7" data-offset="18" data-load-number="4" data-resulting-class="fourth-post" data-exclude="MzE4Mzk5LDMxODM2MCwzMTc2MDAsNjUyNjEsNjQzMTMsNjQzMTgsNjQyNzMsNjMyNTc=">Load More </a>
</div>
Solution
This is somewhat tricky.
The "Load more" button is initially out of the screen here, so you need to scroll the page in order to click that button. But when you scroll this page it scrolls more than needed so the "Load more" button appears out of the screen again, so this time you will have to scroll back :)
"Load more" button presents on the page even when nothing to load more. In this case this button just becomes invisible. So, to stop scrolling you need to check style
attribute of that element.
Also, you need to improve your locators. Very long absolute XPaths are extremely breakable and not reliable.
The following code works!
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)
url = "https://www.architectural-review.com/buildings/house"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.cmplz-btn.cmplz-accept'))).click()
while True:
load_more = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "cpb-bottom-more-news-link")))
load_more.location_once_scrolled_into_view
time.sleep(0.4)
driver.execute_script("window.scrollBy(0, arguments[0]);", -300)
time.sleep(0.4)
displayed = load_more.get_attribute("style")
if "none" in displayed:
break
driver.execute_script("arguments[0].click();", load_more)
Answered By - Prophet
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.