我正在使用Python3和Selenium firefox提交表单,然后获取他们随后登陆的URL。我这样做是
inputElement.send_keys(postnumber)
inputElement.submit()
time.sleep(5)
# Get Current URL
current_url = driver.current_url
print ( " URL : %s" % current_url )
这大部分时间都在运行,但有时页面加载的时间超过5秒,我得到旧的网址,因为尚未加载新网址。
我该怎么做?
答案 0 :(得分:6)
在我的代码中,我创建了一个上下文管理器,它执行以下操作:
html
元素的引用变为陈旧(这意味着页面已开始重新加载)document.readyState
“完成”(这意味着页面已完成初始加载)如果页面中包含使用其他ajax调用填充的内容,我可能会在此之后添加另一个等待,因为我知道在上述四个步骤之后不会立即出现该元素。
有关详细说明,请参阅此博文:How to get Selenium to wait for page load after a click
答案 1 :(得分:4)
url_changes
的 expected_conditions
助手正是用于此目的:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# some work on current page, code omitted
# save current page url
current_url = driver.current_url
# initiate page transition, e.g.:
input_element.send_keys(post_number)
input_element.submit()
# wait for URL to change with 15 seconds timeout
WebDriverWait(driver, 15).until(EC.url_changes(current_url))
# print new URL
new_url = driver.current_url
print(new_url)
答案 2 :(得分:2)
尝试以下方法:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
title = driver.title
inputElement.send_keys(postnumber)
inputElement.submit()
wait(driver, 15).until_not(EC.title_is(title))
current_url = driver.current_url
print ( " URL : %s" % current_url )
这将允许您在提交表单后获得新的URL
,等待最多15秒,直到更改页面标题(,以防新旧页面上有不同的标题)。如果要在新页面上处理元素,则可能需要使用以下代码:
inputElement.send_keys(postnumber)
inputElement.submit()
text_of_element_on_new_page = wait(driver, 15).until(EC.presence_of_element_located((By.ID, "some_element_id"))).text
print ( " Text of element is : %s" % text_of_element_on_new_page )
答案 3 :(得分:1)
driver.find_element_by__link_text('Next').click()
点击链接后,按钮进入新页面,您可以:
等到某个元素出现在旧页面中而不是新页面中;
WebDriverWait(driver, 600).until(expected_conditions.presence_of_element_located((By.XPATH, '//div[@id="main_message"]//table')))
# or just wait for a second for browser(driver) to change
driver.implicitly_wait(1)
正在加载(或加载)新页面时,现在您可以通过执行javascript脚本检查其readyState,当页面加载时,该脚本会输出“完成”消息(值)。
def wait_loading():
wait_time = 0
while driver.execute_script('return document.readyState;') != 'complete' and wait_time < 10:
# Scroll down to bottom to load contents, unnecessary for everyone
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
wait_time += 0.1
time.sleep(0.1)
print('Load Complete.')
这个想法在我的情况下为我说了算,我认为它可以适合大多数情况,而且很容易。
导入StaleElementReferenceException
def wait_for(condition_function):
start_time = time.time()
while time.time() < start_time + 10:
if condition_function:
return True
else:
time.sleep(0.1)
raise Exception(
'Time out, waiting for {}'.format(condition_function.__name__)
)
def click_xpath(xpath):
link = driver.find_element_by_xpath(xpath)
link.click()
def link_staled():
try:
link.find_element_by_id('seccode_cSA')
return False
except StaleElementReferenceException:
return True
wait_for(link_staled())
click_xpath('//button[@name="loginsubmit"]')
此方法来自“ https://blog.codeship.com/get-selenium-to-wait-for-page-load/”(可以从其他地方共享)