Selenium click()无需等待页面加载Python [英] Selenium click() without waiting for page to load Python
问题描述
我正在尝试编写一个Python程序来帮助在网站上进行预订.我知道可以预约的具体时间. 我希望使用Selenium来完成此任务.我的策略是打开10个单独的浏览器窗口,然后让它们以0.1秒的间隔单击提交",从可以进行预订的特定时间的.02秒开始. 我的问题是,每个click()命令似乎都在等待页面加载,然后再移至下一个click()命令.是否可以单击()然后继续执行下一个命令,而无需等待页面加载?这些页面将被加载,因为它们位于不同的窗口中,我计划在此之后手动与它们进行交互.这就是我所拥有的.我希望click()命令立即启动,但不要启动.
I am trying to write a Python program to aid in getting a reservation on a website. I know the specific time a reservation will become available. I hope to use Selenium to accomplish this. My strategy is to open 10 separate browser windows and then have them click "submit" at 0.1second intervals starting .02 seconds before the specific time the reservation is able to be made. The problem I have is that each click() command seems to wait for the page to load before moving to the next click() command. Is it possible to click() and then move on to the next command without waiting for the page to load? The pages will load because they are in different windows, and I plan on interacting with them manually after that. Here's what I have. I want the click() commands to fire immediately but they don't.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
browser1 = webdriver.Firefox()
browser1.get('http://www.reservationsite.com')
browser2 = webdriver.Firefox()
browser2.get('http://www.reservationsite.com')
browser3 = webdriver.Firefox()
browser3.get('http://www.reservationsite.com')
def firstclick():
elem1 = browser1.find_element_by_id('btnbookdates').click()
return
def secondclick():
elem2 = browser2.find_element_by_id('btnbookdates').click()
return
def thirdclick():
elem3 = browser3.find_element_by_id('btnbookdates').click()
return
firstclick()
secondclick()
thirdclick()
推荐答案
You can try loading Firefox with unstable
load strategy (not tested):
有beta功能可以使Firefox不必等待整个页面 在调用.get或.click之后加载.这可能会导致立即查找 中断,因此请确保也使用隐式或显式等待.这 仅适用于Firefox,不适用于其他浏览器.
There is beta feature to make firefox not wait for the full page to load after calling .get or .click. This may cause immediate find's to break, so please be sure to use an implicit or explicit wait too. This is only available for Firefox and not other browsers.
profile = webdriver.FirefoxProfile()
profile.set_preference("webdriver.load.strategy", "unstable")
driver = webdriver.Firefox(firefox_profile=profile)
使用显式等待将其组合以通过id查找元素:
Combine it with an Explicit Wait to find the element by id:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "btnbookdates"))
)
element.click()
作为一种替代选择,有些疯狂"的选择是使用 sikuli
屏幕自动化工具单击指定的图像(例如,按钮的屏幕快照)或屏幕上的指定坐标.
As an alternative and a bit "crazy" option would be to use sikuli
screen automation tool to click at the specified image (screenshot of a button, for example) or at the specified coordinates on the screen.
这篇关于Selenium click()无需等待页面加载Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!