点击“显示更多交易"在带有硒的网页中 [英] 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 the following error:
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()
有什么主意吗?
推荐答案
要在https://www.uswitch.com/broadband/compare/deals_and_offers/
页上单击带有显示10个额外交易的文本的元素,您可以使用以下解决方案:
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)
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
这篇关于点击“显示更多交易"在带有硒的网页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文