机器人硒,Python。时间。睡觉(n)

2024-04-26 00:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如果需要解析并单击DOM元素,还有比selenium更好的方法吗?在

开放式浏览器是可选的(不需要)。在

有更好的选择吗?在

顺便说一句

为什么这个time.sleep(n)后面会出现错误。在

错误:

Traceback (most recent call last):
  File "main.py", line 39, in <module>
    print("Buy: " + str(elemGreen.text))
  File "C:\Users\Andrew\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 76, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "C:\Users\Andrew\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Andrew\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Andrew\AppData\Local\Programs\Python\Python37-32\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
  (Session info: chrome=69.0.3497.100)

我的Python代码:

^{pr2}$

Tags: inpylibpackageslocalseleniumlinesite
1条回答
网友
1楼 · 发布于 2024-04-26 00:38:32

expected_conditions.visibility_of_all_elements_located句柄StaleElementReferenceException。您可以使用它来定位元素

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

def loginCheck():
    elements = WebDriverWait(driver, 30).until(expected_conditions.visibility_of_all_elements_located((By.CLASS_NAME, 'not-login')))
    l = elements[0]
    if (l.text == "Login or Join Trading"):
        return True
    else:
        return False

相关问题 更多 >