Python 点击;“显示更多交易”;在网页中添加硒
我想点击下一页上的每一个“显示10个更多交易”:“但它似乎不起作用。 我遇到了以下错误:Python 点击;“显示更多交易”;在网页中添加硒,python,selenium,web-scraping,lazy-loading,webdriverwait,Python,Selenium,Web Scraping,Lazy Loading,Webdriverwait,我想点击下一页上的每一个“显示10个更多交易”:“但它似乎不起作用。 我遇到了以下错误: AttributeError: 'NoneType' object has no attribute 'find_element' 我的代码如下: from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import e
AttributeError: 'NoneType' object has no attribute 'find_element'
我的代码如下:
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/"
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()
有什么想法吗?试试下面的方法,它使用CSS属性=值选择器将按钮的
数据事件操作
属性作为其值的目标
driver.find_element_by_css_selector('[data-event-action="Show 10 more products"]').click()
如果需要,将
driver
替换为browser
。单击文本为的元素,在https://www.uswitch.com/broadband/compare/deals_and_offers/
您可以使用以下解决方案:
- 代码块:
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) 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()
- 控制台输出:
Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked Button clicked No more Buttons
while not 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)
这避免了最终会让您发疯的selenium错误。谢谢。我最终得到了以下错误:WebDriverException:未知错误:元素。。。在点(981920)处不可单击。其他元素将收到点击:@sammtt检查我的更新答案,并让我知道状态