Click on Show more deals in webpage with Selenium

2024/11/18 2:32:15

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 the following error:

 AttributeError: 'NoneType' object has no attribute 'find_element'

My code is the following:

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 Byurl = "https://www.uswitch.com/broadband/compare/deals_and_offers/"driver = webdriver.Chrome(r'C:\temp\chromedriver.exe')browser = driver.get(url)
while True:button = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Show 10 more deals')))
button.click()

Any idea?

Answer

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 Byurl = "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)
    while True: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
    
https://en.xdnf.cn/q/120123.html

Related Q&A

In Matplotlib, what axis attribute specifies the spacing between ticks? [duplicate]

This question already has answers here:Changing the tick frequency on the x or y axis(15 answers)Closed 6 years ago.When generating a Matplotlib line or scatter plot, what axis attribute specifies the …

How can I make a simple calculator in python 3?

Im making this calculator using python 3, and this is what I have so far:print("Welcome to Calculator!")class Calculator:def addition(self,x,y):added = x + yreturn addeddef subtraction(self,x…

python 3: lists dont change their values

So I am trying to change a bunch of list items by a random percentage using a for loop.import random as rdm list = [1000, 100, 50, 25] def change():for item in list:item = item + item*rdm.uniform(-10, …

Using zip_longest on unequal lists but repeat the last entry instead of returning None

There is an existing thread about this Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped But its not quite Im after. Instead of returning None, …

python scrapy not crawling all urls in scraped list

I am trying to scrape information from the pages listed on this page. https://pardo.ch/pardo/program/archive/2017/catalog-films.htmlthe xpath selector:film_page_urls_startpage = sel.xpath(//article[@cl…

Python - Do (something) when event is near [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python script to find nth prime number

Im new to Python and I thought Id try to learn the ropes a bit by writing a function to find the nth prime number, however I cant get my code to work properly. No doubt this is due to me missing someth…

Printing values from list within an input range

I have an unordered list, lets say:lst = [12,23,35,54,43,29,65]and the program will prompt the user to input two numbers, where these two numbers will represent the range.input1 = 22input2 = 55therefor…

An issue with the tag add command of the ttk.Treeview widget - cant handle white space

I have noticed an issue with using the tag add command of a ttk.Treeview widget when activated with the tk.call() method. That is, it cant handle white space in the value of the str() elements of its i…

How to show the ten most overdue numbers in a list

I have asked a question before about this bit of code and it was answered adequately, but I have an additional question about showing the ten most overdue numbers. (This program was a part of an in-cla…