我想selenium判断页面上的一些文本,返回true,不是false

时间:2018-04-29 04:00:03

标签: selenium selenium-webdriver selenium-chromedriver

我希望selenium判断一个页面上的一些文本,返回true,no false

例如,我创建了一个函数来确定某个文本段落是否出现在网页上。如果出现此文本,则会返回' true'否则返回' flase'。但我不知道如何使用“硒”#39;写下这个,下面的代码是我试着写的。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
n=1
driver = 
webdriver.Chrome('F:\pythonjiaoben\chromedriver_win32\chromedriver.exe')
driver.get('this is my game url')
def is_text(a):
    try:
       s=driver.find_element_by_link_text(a)
       return True
    except Exception as e:
        return False

elem=driver.find_element_by_partial_link_text("地狱之门[永恒]")
elem.click()
driver.implicitly_wait(10)
while(n>0):
    if is_text("您点击的太快了,请等会再刷新!"):
        elem=driver.find_element_by_partial_link_text("返回游戏")
        elem.click()
        continue
    if is_text("进入世界"):
        elem=driver.find_element_by_partial_link_text("进入世界")
        elem.click()
        continue
    break

显示这些错误

Traceback (most recent call last):
  File "jiaoben.py", line 24, in 
    elem.find_element_by_partial_link_text("进入世界")
  File "C:\Users\Administrator\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 273, in find_element_by_partial_link_text
    return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)
  File "C:\Users\Administrator\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 653, in find_element
    {"using": by, "value": value})['value']
  File "C:\Users\Administrator\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Administrator\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Administrator\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

0 个答案:

没有答案