【发布时间】:2015-02-23 20:31:45
【问题描述】:
我正在尝试制作一个 python webdriver 来加载网页,然后断言 true 并在文本或对象存在时运行打印命令,如果没有,我希望它继续运行我的循环。我是 python 的菜鸟,一直在学习 Learn python the Hard Way,并阅读文档。我花了最后一天左右的时间试图让我的代码查找文本或元素,但它没有反馈信息......这是我的代码到目前为止减去关于访问网页的顶部部分,我只是这个计数循环中的东西断言逻辑。
count = 1000.00
while count < 1000.03:
driver.find_element_by_id("select").clear()
driver.find_element_by_id("select").send_keys(str(count))
time.sleep(1)
driver.find_element_by_id("BP").click()
driver.find_element_by_id("BP").click()
count += 0.01 ## increases count to test
highervalue = driver.find_element_by_link_text("Select Error")
assertTrue(driver.link_contains_match_for("")) ##could also be, ##text_containt_match_for("ex") or driver.assertTrue(element in WEBPAGE)??
print 'Someone is %.7r' %count
else:
print 'I have %.7r' %count
time.sleep(1)
然后循环重新开始。我遇到的问题是我想以某种形式、链接或文本在网页上找到“选择错误”,然后如果它在那里打印我一条消息,如果没有,就继续我的循环。
使用 assert/asserttrue 或者类似的东西会更好吗
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
或
我搜索过的其他一些可以使用的例子:
self.assertTrue(self.is_element_present(By.ID, "FOO"))
self.assertTrue(self.is_element_present(By.TEXT, "BAR"))
self.assertTrue(self.is_text_present("FOO"))
self.assertTrue(self.driver.is_text_present("FOO"))
当我在网页中找到某些内容时,有人可以告诉我如何编写该部分并且如果找到它会给我反馈吗?
【问题讨论】:
标签: python selenium webdriver assert