WebDriver,Python:如何使用适当的值捕获动态更新URL并将其保存到变量中

时间:2016-02-18 12:29:42

标签: python selenium selenium-webdriver webdriver robotframework

python / selenium web驱动程序代码:

elem = driver.find_element_by_css_selector("#username")
elem.send_keys("username")
elem = driver.find_element_by_css_selector("#password")
elem.send_keys("password")

driver.implicitly_wait(2) #seconds
elem = driver.find_element_by_css_selector("button")
elem.click()

def condition(driver):
    look_for = ("url_which_contains_this_text")
    url = driver.current_url
    for look_for in url:
        if url.find(look_for) != -1:
            print url

    return url

#page_url = driver.current_url
print url

在此代码中: 1)用户登录; 2)点击“登录按钮”; 然后我需要以某种方式捕捉动态变化的URL 每秒(加载访问令牌等),并捕获其中的URL 包含值“example_id =”并将此URL保存到变量并打印。

有人帮助我吗?

1 个答案:

答案 0 :(得分:2)

我不确定这是否可行,因为我无法测试,但您可以尝试这样的事情:

required_url = ""
while True:
    current_url = driver.current_url
    if "example_id=" in current_url:
        required_url = current_url
        print('\n'+required_url)
        break
    else: print(current_url)