简体   繁体   English

如何在使用 python 截屏之前通过请求?

[英]How to pass the request before take screenshot with python?

I run the code to get maps image as below我运行代码以获取地图图像,如下所示

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get('https://www.google.se/maps/@-0.1921083,-78.4865351,17.25z/data=!5m1!1e1')
sleep(1)

driver.get_screenshot_as_file("screenshot.png")
driver.quit()
print("end...")

The results is as结果如下在此处输入图像描述

How to get only the maps picture without the dialog in the picture?如何仅获取地图图片而没有图片中的对话框? I try to pass it but it do not work.我试图通过它,但它不起作用。

the pop up is弹出窗口是在此处输入图像描述

The hltm from the link is as picture链接中的 hltm 如图在此处输入图像描述

xpath is /html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span xpath 是/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span

I run with new version我运行新版本

from selenium import webdriver
from time import sleep

PATH = "https://www.google.se/maps/@-0.1921083,-78.4865351,17.25z/data=!5m1!1e1"
driver = webdriver.Firefox()

driver.get(PATH)
sleep(2)
xpath = '/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span'
print(xpath)
#Xpath=//tagname[@attribute='value']
elem  = driver.find_element_by_xpath(xpath)
sleep(1)
elem.click()
sleep(2)
driver.get_screenshot_as_file("screenshot.png")
driver.quit()
print("end...")

The error is NoSuchElementException: Message: Unable to locate element: /html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span错误是NoSuchElementException: Message: Unable to locate element: /html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span

The "Jag godkänner" button is in a different iframe. “Jag godkänner”按钮位于不同的 iframe 中。 Since there is only one iframe in the page, you can just find the first element with the iframe tag.由于页面中只有一个 iframe,因此您只需找到带有iframe标签的第一个元素。

You can also remove all of the sleep .你也可以去掉所有的sleep

This code works:此代码有效:

from selenium import webdriver

PATH = "https://www.google.se/maps/@-0.1921083,-78.4865351,17.25z/data=!5m1!1e1"
driver = webdriver.Firefox()

driver.get(PATH)
xpath = '/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span'
print(xpath)

# I added these two lines to your last example:
frame = driver.find_element_by_tag_name("iframe") # Finds iframe
driver.switch_to.frame(frame) # Switches to that iframe

#Xpath=//tagname[@attribute='value']
elem  = driver.find_element_by_xpath(xpath)
elem.click()
driver.get_screenshot_as_file("screenshot.png")
driver.quit()
print("end...")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM