Selenium返回的html源代码与在浏览器中查看的不同
我正试图使用Selenium通过单击此处的“加载更多”按钮来加载下一页的结果。 但是,selenium加载的html页面的源代码没有显示(加载)浏览时可以看到的实际产品。 这是我的密码:Selenium返回的html源代码与在浏览器中查看的不同,selenium,webdriver,phantomjs,Selenium,Webdriver,Phantomjs,我正试图使用Selenium通过单击此处的“加载更多”按钮来加载下一页的结果。 但是,selenium加载的html页面的源代码没有显示(加载)浏览时可以看到的实际产品。 这是我的密码: from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By from selenium
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#browser = webdriver.Firefox()#Chrome('./chromedriver.exe')
URL = "https://thekrazycouponlady.com/coupons-for/costco"
PATIENCE_TIME = 60
LOAD_MORE_BUTTON_XPATH = '//button[@class = "kcl-btn ng-scope"]/span'
caps = DesiredCapabilities.PHANTOMJS
# driver = webdriver.Chrome(r'C:\Python3\selenium\webdriver\chromedriver_win32\chromedriver.exe')
caps["phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
driver = webdriver.PhantomJS(r'C:\Python3\selenium\webdriver\phantomjs-2.1.1-windows\bin\phantomjs.exe',service_log_path=os.path.devnull,desired_capabilities=caps)
driver.get(URL)
while True:
try:
time.sleep(20)
html = driver.page_source.encode('utf-8')
print(html)
loadMoreButton = driver.find_element_by_xpath(LOAD_MORE_BUTTON_XPATH)
loadMoreButton.click()
except Exception as e:
print (e)
break
print ("Complete")
driver.quit()
不确定我是否可以在此附加示例html文件以供参考。
无论如何,问题是什么?我如何使用selenium加载与通过浏览器加载完全相同的页面?这可能是由于使用了PhantomJS,它不再维护,并且已从selenium 3.8.1中弃用。改用镀铬无头
options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
这可能是由于使用了PhantomJS,它不再被维护,并且从Selenium 3.8.1中被弃用。改用镀铬无头
options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
Selenium返回的代码是什么意思?@Guy是Selenium加载的html页面的源代码。Selenium返回的代码是什么意思?@Guy是Selenium加载的html页面的源代码。谢谢。返回了正确的内容。虽然现在我得到了2个其他错误-是不可点击点(391579)和元素不可交互。我在页面上看到一些横幅。这会是个问题吗?@BillyJhon是的。如果横幅自动消失,你可以等待它消失,看一看,类似于
WebDriverWait(driver,10)。直到(EC.invisibility\u of_element\u located((By.ID,“some ID”))
。否则你需要关闭它。谢谢。这返回了正确的内容。虽然现在我收到了另外两个错误-此时无法单击(391579)和元素不可交互。我在页面上看到一些横幅。这可能是个问题吗?@BillyJhon是的。如果横幅自动消失,你可以等待它消失,看看,类似于WebDriverWait(driver,10)。直到(EC.invisibility\u of_Element\u located((By.ID,“some ID”)
。否则你需要关闭它。