Click On "show More Deals" In Webpage With Selenium
I'd like to click on every 'Show 10 more deals' on the following page: 'https://www.uswitch.com/broadband/compare/deals_and_offers/' but it does not seem to work. I'm stuck having
Solution 1:
To click on the element with text as Show 10 more deals on the page https://www.uswitch.com/broadband/compare/deals_and_offers/
you can use the following solution:
Code Block:
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By url = "https://www.uswitch.com/broadband/compare/deals_and_offers/" options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument('disable-infobars') browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') browser.get(url) whileTrue: try: browser.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(browser,20).until(EC.visibility_of_element_located((By.XPATH, "//button[@class='us-btn us-btn--action' and contains(.,'Show 10 more deals')]")))) browser.execute_script("arguments[0].click();", WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.us-btn.us-btn--action[name='visible_products']")))) print("Button clicked") except: print("No more Buttons") break browser.quit()
Console Output:
Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked No more Buttons
Solution 2:
Try the following which uses a CSS attribute = value selector to target the data-event-action
attribute of the button by its value
driver.find_element_by_css_selector('[data-event-action="Show 10 more products"]').click()
Replace driver
with browser
if required.
Solution 3:
Try it like this:
whilenot re.search(r"Showing (\d+) of \1 ", browser.page_source):
browser.execute_script("document.querySelector('[data-event-label=\"Show 10 more products\"]').click()")
time.sleep(1)
This avoids the selenium errors that will eventually drive you nuts.
Post a Comment for "Click On "show More Deals" In Webpage With Selenium"