Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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 Selenium-等待表单提交后加载下一页_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python Selenium-等待表单提交后加载下一页

Python Selenium-等待表单提交后加载下一页,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在使用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秒加载,我得到了旧的网址

我正在使用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秒加载,我得到了旧的网址,因为新的还没有加载


我应该如何做到这一点?

尝试以下方法:

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 )
这将允许您在表单提交后最多等待15秒,直到页面标题更改(如果新页面和旧页面上有不同的标题),以获取新的
URL
。若要在新页面上处理元素,则可能需要使用以下代码:

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 )

在我的代码中,我创建了一个执行以下操作的上下文管理器:

  • 获取对“html”元素的引用
  • 提交表格
  • 等待对
    html
    元素的引用过期(这意味着页面已开始重新加载)
  • 等待
    document.readyState
    完成(这意味着页面已完成初始加载)
如果页面中有其他ajax调用填充的内容,我可能会在这之后添加另一个等待,等待我知道在上述四个步骤之后不会立即出现的元素

有关详细说明,请参阅此博文:

Method1 单击链接按钮转到新页面后,您可以:

等待出现新页面中的元素,而不是旧页面中的元素

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.')
def wait_load():
等待时间=0
而driver.execute_脚本('return document.readyState;')!='完成并等待时间<10:
#向下滚动至底部以加载内容,每个人都不需要
执行脚本(“window.scrollTo(0,document.body.scrollHeight);”)
等待时间+=0.1
睡眠时间(0.1)
打印('加载完成')
在我的案例中,这个想法很适合我,我认为它可以适合大多数情况,而且很简单

方法2 从selenium.common.exceptions导入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"]')
def wait_for(条件功能):
开始时间=time.time()
while time.time()

此方法来自“”(可以从其他地方共享)

url\u更改
helper来自
expected\u条件
正是出于此目的:

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)

您无法获取当前url=driver的错误。当前url
。。。这将只返回当前或新页面
URL
…很好,op updated您应该指出,只有当新页面的标题与当前页面的标题不同时,此解决方案才有效。我曾在几个系统上工作过,但情况并非如此。哇,这是一个超级简单的解决方案-它可以用于任何
预期的\u条件
,因此在我的例子中,我使用它来检查新的URL:
WebDriverWait(驱动程序,15)。直到(预期的\u条件。URL\u改变()http://demo.com/newUrl)
。作为魅力的作品:)
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)