Python 3.x 我如何告诉我的python脚本等待;另存为;在执行下一个代码块之前,是否在chrome中弹出对话框?
我使用selenium和pyautogui从网站下载图片。我的代码的逻辑是使用selenium在我想要的链接页面中导航。通过使用pyautogui,我可以将它们结合起来,自动右键单击并下载图像(虽然不是很完美),然后输入文件名。我现在面临的问题是,如果保存图像的对话框需要很长时间才能弹出。Pyautogui不能等待它弹出,而只是盲目地输入它应该输入的文本。我很难找到这个,也很难找到一种方法来检测弹出的对话。我现在被这个问题困扰着。有没有什么方法可以让我的程序等待它在selenium中弹出?还是我必须使用另一个模块?在这种情况下,我可以使用什么模块 重现问题的代码:Python 3.x 我如何告诉我的python脚本等待;另存为;在执行下一个代码块之前,是否在chrome中弹出对话框?,python-3.x,selenium,web-scraping,download,pyautogui,Python 3.x,Selenium,Web Scraping,Download,Pyautogui,我使用selenium和pyautogui从网站下载图片。我的代码的逻辑是使用selenium在我想要的链接页面中导航。通过使用pyautogui,我可以将它们结合起来,自动右键单击并下载图像(虽然不是很完美),然后输入文件名。我现在面临的问题是,如果保存图像的对话框需要很长时间才能弹出。Pyautogui不能等待它弹出,而只是盲目地输入它应该输入的文本。我很难找到这个,也很难找到一种方法来检测弹出的对话。我现在被这个问题困扰着。有没有什么方法可以让我的程序等待它在selenium中弹出?还是我
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pyautogui as auto
driver = webdriver.Chrome()
urls = [ # Sample urls.
'https://unsplash.com/photos/MAqmEdUCq4k',
'https://unsplash.com/photos/s7PhRjUJNeA',
]
# Declaring img_xpath and x for loop counting.
img_xpath = '//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div/button/div[2]/img'
x = 0
for url in urls: # Loops through urls.
x += 1
time.sleep(2)
driver.get(url)
# Waits for the image element of the site to load.
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, img_xpath))
)
# Calculates current browser center relative to screen position.
win_pos = driver.get_window_position()
win_size = driver.get_window_size()
bcs = [ # bcs stands for Browser Center on Screen.
win_pos['x'] + int(win_size['width'] / 2),
win_pos['y'] + int(win_size['height'] / 2),
]
# Using pyautogui to save image.
auto.moveTo(bcs[0], bcs[1])
auto.click()
time.sleep(1)
auto.rightClick()
auto.moveTo(bcs[0] + 25, bcs[1] + 35)
auto.click()
time.sleep(2) # Comment out this line to reproduce the problem if your system is too fast.
auto.typewrite(f'Sample_{str(x)}')
auto.press('enter')
time.sleep(3)
driver.close()
额外说明。代码使用带有默认设置的Chrome浏览器
版本:
蟒蛇→ 3.8.5
皮奥图吉→ 0.9.50
硒→ 3.141.0
如果我问错了什么,我道歉。这是我在网站上的第一个问题。另外,我对python还是个新手。我已经解决了这个问题。我使用了一个名为“pywinauto”的模块,并使用了应用程序模块。然后我们转到一个循环,使用connect()方法连接到浏览器,并使用exists()方法判断对话框是否存在。我将这两种方法都放在循环中,每秒钟左右更新一次。然后,一旦检测到对话框,我们将继续余下的过程。此模块还有其他方法来判断对话框是否存在以及是否位于屏幕顶部。但这个解决方案对我有效 守则:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pyautogui as auto
from pywinauto.application import Application as App
import warnings # Import module to remove warnings
driver = webdriver.Chrome()
urls = [ # Sample urls.
'https://unsplash.com/photos/MAqmEdUCq4k',
'https://unsplash.com/photos/s7PhRjUJNeA',
]
# Declaring img_xpath and x for loop counting.
img_xpath = '//*[@id="app"]/div/div[2]/div/div[1]/div[3]/div/div/button/div[2]/img'
x = 0
# Ignore UserWarning.
warnings.simplefilter('ignore', category=UserWarning)
for url in urls: # Loops through urls.
x += 1
time.sleep(2)
driver.get(url)
# Waits for the image element of the site to load.
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, img_xpath))
)
# Declaring program path.
chrome = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
# Calculates current browser center relative to screen position.
win_pos = driver.get_window_position()
win_size = driver.get_window_size()
bcs = [ # bcs stands for Browser Center on Screen.
win_pos['x'] + int(win_size['width'] / 2),
win_pos['y'] + int(win_size['height'] / 2),
]
# Using pyautogui to save image.
auto.moveTo(bcs[0], bcs[1])
auto.click()
time.sleep(1)
auto.moveTo(bcs[0], bcs[1])
auto.rightClick()
auto.moveTo(bcs[0] + 25, bcs[1] + 35)
auto.click()
y = 0
while y != 10:
y += 1
time.sleep(1)
app = App().connect(path=chrome) # Connects script to Chrome.
if app['Save As'].exists(): # Checks dialog box 'Save As' existence.
auto.typewrite(f'Sample_{str(x)}')
auto.press('enter')
break
time.sleep(3)
driver.close()
附加说明。我已经对代码进行了一点重构,以便更流畅地工作。所有的预设和设置都差不多
pywinauto版本→ 0.6.8