1

我成功创建了一个从图表平台 tradingview 下载股票数据的 python 脚本。当我从本地机器启动它时,一切正常。

现在我想从 ubuntu ec2 机器和 crontab 启动脚本。几天前,我第一次尝试从 ec2 机器手动运行脚本。今天我想尝试设置 crontab。它没有用,所以我尝试再次手动启动 python 脚本。这次我得到这个错误。

File "automated_prediction_ec2.py", line 282, in <module>
    signin.click()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: headless chrome=86.0.4240.75)

首先我认为可能是因为 tradingview 改变了他们的 HTML,所以我尝试从我的本地机器上运行它。这和以前一样完美。

这些是我的 ChromeOptions。我已经尝试了几种组合来解决问题,但这些是上次有效的组合。

options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : '/home/ubuntu/daily_stock_data'}
options.add_argument('--headless')
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
options.add_experimental_option('prefs', prefs)

那么可能是什么问题,有一天我的脚本在 ec2 机器上运行,然后突然我得到了这个错误?此外,该脚本正在下载 130 个 CSV。ec2 实例是否有下载限制之类的东西?

这是发生错误的代码。

driver.get("https://www.tradingview.com/")
driver.implicitly_wait(20)
signin = driver.find_element_by_class_name("tv-header__link.tv-header__link--signin.js-header__signin")
signin.click()

页面的 HTML 片段。

<span class="tv-header__dropdown-text">
<a class="tv-header__link tv-header__link--signin js-header__signin" href="#signin">Sign in</a>
</span>
4

1 回答 1

0

要单击带有文本作为登录的元素,您可以使用以下任一定位器策略

  • 使用link_text

    driver.find_element_by_link_text("Sign in").click()
    
  • 使用css_selector

    driver.find_element_by_css_selector("a.tv-header__link--signin[href='#signin']").click()
    
  • 使用xpath

    driver.find_element_by_xpath("//a[contains(@class, 'tv-header__link--signin') and text()='Sign in']").click()
    

理想情况下,要单击需要诱导WebDriverWait的元素element_to_be_clickable(),您可以使用以下任一Locator Strategies

  • 使用LINK_TEXT

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Sign in"))).click()
    
  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.tv-header__link--signin[href='#signin']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'tv-header__link--signin') and text()='Sign in']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
于 2020-11-11T22:11:58.550 回答