Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 点击后的类似屏幕截图_Python_Html_Selenium_Web Scraping_Phantomjs - Fatal编程技术网

Python 点击后的类似屏幕截图

Python 点击后的类似屏幕截图,python,html,selenium,web-scraping,phantomjs,Python,Html,Selenium,Web Scraping,Phantomjs,我正在尝试拍摄使用Selenium的截图。但由于Chrome和Firefox不允许完整的页面截图,我使用的是PhantomJS 有两组持续时间:12个月和按月。因此,我尝试单击每个选项卡并截图 获取页面内容的代码为: browser = webdriver.PhantomJS() browser.set_window_size(1366, 728) browser.get("http://www.optus.com.au/shop/broadband/mobile-broadband/data-

我正在尝试拍摄使用Selenium的截图。但由于Chrome和Firefox不允许完整的页面截图,我使用的是PhantomJS

有两组持续时间:
12个月
按月
。因此,我尝试单击每个选项卡并截图

获取页面内容的代码为:

browser = webdriver.PhantomJS()
browser.set_window_size(1366, 728)
browser.get("http://www.optus.com.au/shop/broadband/mobile-broadband/data-sim-card")
delay = 30 # seconds
try:
    wait = WebDriverWait(browser, delay)
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.price")))
    print("\nPage is ready!")
except TimeoutException:
    print("Loading took too much time!")

html = browser.page_source
soup = BeautifulSoup(html, "html.parser")
要获取持续时间的css类名,请执行以下操作:

durations = soup.body.find("ul", attrs={'class': 'filter-options grouped'})
duration_filtrs = {}
for content in durations.contents:
    duration = content.text  # Storage of the model 64GB, 256GB, 512GB
    css_clss = list(filter(lambda x: x not in ['', 'active'], content.attrs['class']))
    filtr_nm = '.' + '.'.join(css_clss)
    duration_filtrs[duration] = filtr_nm

print(duration_filtrs) 
# {'12 Months': '.filter-option.contract_length_12', 'Month to Month':'.filter-option.contract_length_1'}
要获取每个持续时间选项卡的屏幕截图

for duration, css_cls in duration_filtrs.items():
    browser.find_element_by_css_selector(css_cls).click()
    browser.save_screenshot(duration+'.png')
使用上面的代码,即使文件大小略有不同,我也会得到类似的屏幕截图


有人能告诉我我做错了什么吗?

我不知道如何在PhantomJS中解决这个问题。我推荐一种铬合金无头的解决方法,如下所示。您只需要指定窗口大小

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=2160,3840") # you can adjust the size as you want
browser = webdriver.Chrome(chrome_options=chrome_options)
...
...
for duration, css_cls in duration_filtrs.items():
    button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,css_cls)))
    browser.save_screenshot('before-'+duration+'.png')
    print(button.text)
    button.click()
    time.sleep(8)
    browser.save_screenshot(duration+'.png')

你的意思是说选项卡似乎没有被切换吗?当我使用Chrome(而不是PhantomJS)时,选项卡会切换,而屏幕截图(即使它们不是整页)对于你的脚本来说,在meThanks Buaban看来是正确的+这是我的荣幸。我从未听说过如何运行
Chrome无头浏览器。按照你的指导方针,我成功了。不过我想知道一件事。与PhantomJS不同,selenium headless browser能在需要时成功单击吗?@SIM当然能。Headless模式与GUI模式的工作原理相同。非常感谢您的回复。谢谢。我真的很想捕捉价格部分的截图。你能告诉我怎么做吗?这些对我有用。Thankss
options=webdriver.ChromeOptions()选项。添加参数(“headless”)选项。添加参数(“windowsize=2160x3840”)。#您可以根据需要调整大小browser=webdriver.Chrome(Chrome\u options=options)