python - 使用 Selenium 的预期条件 [Python]

标签 python selenium

我想加载我的驱动程序,直到我的 current_url 包含“某些内容”。我尝试过以下代码:

self.url = self.driver.current_url
try:
    element = WebDriverWait(self.driver, 20).until(EC.title_contains("XXX", "YYY", "ZZZ"))
except:
    print "\n IMPERFECT URL \n"
finally:
    self.driver.quit()

但是这种方法使用标题搜索..我想检查我当前的网址是否有可能的字符串集。我怎么做?另外我想检查同一网址中的三组字符串。有人可以帮忙吗。我是 Selenium 的新手。

最佳答案

我不太明白你想要执行什么确切的测试。无论如何,您可以通过可调用并测试您想要的任何内容。下面是一个工作代码示例,用于测试当前 URL 中是否存在 googleblahfoo:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome("/home/ldd/src/selenium/chromedriver")

driver.get("http://google.com")

def condition(driver):
    look_for = ("google", "blah", "foo")
    url = driver.current_url
    for s in look_for:
        if url.find(s) != -1:
            return True

    return False

WebDriverWait(driver, 10).until(condition)

driver.quit()

(显然,Chrome驱动程序的路径要根据自己的情况进行调整。)

一旦condition的返回值为真值,等待就结束。否则,将引发TimeoutException。如果您从 ("google", ...) 中删除 "google",您将收到 TimeoutException

关于python - 使用 Selenium 的预期条件 [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455789/

相关文章:

python - 我如何让我的 python 代码从我的吃 bean 人游戏中删除药丸?

python - 如何在函数中使用 LOCAL VARIABLE,就像其他函数的 GLOBAL VARIABLE 变量一样

Selenium Webdriver sessionId 或检查所有浏览器窗口是否关闭

python - 如何捕获 python 中的非 fatal error

python - 为不同的文件下载更新 download.default_directory chromedriver

python - Selenium - 在脚本中提取 onclick 事件目标 url

javascript - 失败 : unknown error: Element is not clickable at point (x, x) - Angular/Protractor

Python BeautifulSoup 找不到表 ID

python - 广播两个不同形状的数组(Numpy-Python)

python - 如何为所有函数和类包含一个 cpp 文件但忽略主要函数?