python - 网络驱动程序,Python : How to catch dynamically updating url with the appropriate value and save it to the variable

标签 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 每秒(加载访问 token 等),并捕获 URL 包含值“example_id=”并将此 url 保存到变量并打印。

有人可以帮助我吗?

最佳答案

我不确定这是否有效,因为我无法测试它,但您可以尝试这样的操作:

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)

关于python - 网络驱动程序,Python : How to catch dynamically updating url with the appropriate value and save it to the variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35481533/

相关文章:

python - 落在网格网格之外的数据点被插值,而网格网格肯定覆盖了这些点

python - 如何在 SymPy 中创建一个参数本身就是随机变量的随机变量?

python - 抓取每个 eBay 列表的有效方法

javascript - 无法使用 Selenium 在文本框中插入文本(使用 firefox)

java - 使用 Selenium 在 Chrome 中获取整页屏幕截图

python - 在python中删除文件的权限问题

python - PIL 可以用于获取 svg 文件的尺寸吗?

javascript - 使用 Selenium 在 Python 中滚动到页面顶部

python - Selenium WD Python 消息 : Invalid timeout type specified: sessionId

c# - Selenium 2/Webdriver - 如何双击表格行(打开一个新窗口)