简体   繁体   English

Python Selenium 无头错误:元素不可交互

[英]Python Selenium Headless Error: Element not Interactable

I was making a program which scrapes info about stocks from the website https://www.tradingview.com/screener/ which used selenium.here is the code:我正在制作一个程序,该程序从使用 selenium 的网站https://www.tradingview.com/screener/ 抓取有关股票的信息。这里是代码:

def stock(aaa):
    a=[]
    chrome_options = Options()
    custom_header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
    chrome_options.add_argument("--headless")
    chrome_options.add_argument(f"user-agent={custom_header}")
    chrome_options.add_argument("--disable-gpu")
   
    browser = webdriver.Chrome(options=chrome_options)
    browser.get("https://www.tradingview.com/screener/")
    entry=browser.find_element_by_css_selector("#js-screener-container > div.tv-data-table-sticky-wrapper.tv-screener-sticky-header-wrapper > table > thead > tr > th.tv-data-table__th.tv-data-table__cell.js-tv-data-table-th.js-tv-data-table-th-name.tv-data-table__sortable.tv-screener-table__sortable.tv-screener-table__th.js-draggable > div > div > div.tv-screener-table__search-query.js-search-query.tv-screener-table__search-query--without-description>input")
    entry.send_keys(aaa)

It was working well when it was not in headless mode,but when I switched to headless,it gave this error code:它在非无头模式时运行良好,但当我切换到无头模式时,它给出了以下错误代码:

File "c:\Users\██████\cowination.py", line 181, in <module>
    aaa=stock("zydus")
  File "c:\Users\██████\cowination.py", line 20, in stock
    entry.send_keys(aaa)
  File "C:\Users\████████████\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "C:\Users\████████████\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\████████████\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\████████████\Python39\lib\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=90.0.4430.212)

At first I thought it was because the window was not maximized as the entry box is not visible when not maximized.So I added the --start-maximized argument,but it too didn't work.I tried even Webdriverwait,but that to didn't work.起初我以为是因为 window 没有最大化,因为输入框在没有最大化时是不可见的。所以我添加了--start-maximized参数,但它也不起作用。我什至尝试了 Webdriverwait,但那没用。 So is there any way I can solve this problem?那么有什么办法可以解决这个问题吗? thanks in advance提前致谢

Edit: I have written my code according to the correct answer.here is the code: from selenium import webdriver编辑:我已经根据正确的答案编写了我的代码。代码如下: from selenium import webdriver

from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
def stock(aaa):
    a=[]
    chrome_options = Options()
    custom_header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
    chrome_options.add_argument("--headless")
    chrome_options.add_argument(f"user-agent={custom_header}")
    chrome_options.add_argument("--disable-gpu")
   
    browser = webdriver.Chrome(options=chrome_options)
    browser.get("https://www.tradingview.com/screener/")
    browser.maximize_window()
    time.sleep(3)
    e=browser.find_element_by_xpath("/html/body/div[6]/div/div[2]/div[8]/div[1]/img")
    e.click()
    from selenium.webdriver.common.action_chains import ActionChains
    ActionChains(browser).move_to_element(browser.find_element_by_css_selector('#js-screener-container > div.tv-screener-toolbar.tv-screener-toolbar--standalone.tv-screener-toolbar--standalone_sticky > div.tv-screener-market-select.tv-dropdown.tv-dropdown--inline.tv-dropdown-behavior.i-opened > div.tv-dropdown__body.tv-dropdown-behavior__body.tv-dropdown__body--over-trigger.tv-dropdown__body--position_right.undefined.i-opened > div > div.tv-dropdown-behavior__inscroll.js-content > div:nth-child(19) > div > span')).perform()
    india=browser.find_element_by_css_selector('#js-screener-container > div.tv-screener-toolbar.tv-screener-toolbar--standalone.tv-screener-toolbar--standalone_sticky > div.tv-screener-market-select.tv-dropdown.tv-dropdown--inline.tv-dropdown-behavior.i-opened > div.tv-dropdown__body.tv-dropdown-behavior__body.tv-dropdown__body--over-trigger.tv-dropdown__body--position_right.undefined.i-opened > div > div.tv-dropdown-behavior__inscroll.js-content > div:nth-child(19) > div > span')
    india.click()
    time.sleep(1)
    filters=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener-toolbar.tv-screener-toolbar--standalone.tv-screener-toolbar--standalone_sticky > div.tv-screener-toolbar__button.tv-screener-toolbar__button--options.tv-screener-toolbar__button--filters.apply-common-tooltip.common-tooltip-fixed")
    filters.click()
    time.sleep(1)
    reset=browser.find_element_by_css_selector("#overlap-manager-root > div > div > div > div.tv-screener-search.js-screener-search > div")
    reset.click()
    exitbut=browser.find_element_by_css_selector("#overlap-manager-root > div > div > div > div.tv-dialog__section.tv-dialog__section--title.tv-tabbed-dialog__header.js-close-button-place > div.tv-dialog__close.js-dialog__close.tv-tabbed-dialog__close")
    exitbut.click()
    time.sleep(3)
    entry=browser.find_element_by_css_selector("#js-screener-container > div.tv-data-table-sticky-wrapper.tv-screener-sticky-header-wrapper > table > thead > tr > th.tv-data-table__th.tv-data-table__cell.js-tv-data-table-th.js-tv-data-table-th-name.tv-data-table__sortable.tv-screener-table__sortable.tv-screener-table__th.js-draggable > div > div > div.tv-screener-table__search-query.js-search-query.tv-screener-table__search-query--without-description>input")
    
    browser.execute_script("$('.tv-screener-table__search-input.js-search-input').val('{}');".format(aaa))
    action = webdriver.ActionChains(browser);
    action.send_keys(Keys.RETURN).send_keys(Keys.BACKSPACE).perform()
    time.sleep(6)
    p=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td.tv-data-table__cell.apply-common-tooltip.tv-screener-table__cell.tv-screener-table__cell--left.tv-screener-table__cell--with-marker > div > div:nth-child(3) > span.tv-screener__description")
    p1=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(2) > span")
    a.append(p.text)
    a.append(p1.text)
    p2=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(3)")
    p3=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(4)")
    a.append(p2.text)
    a.append(p3.text)
    p4=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(5) > span")
    a.append(p4.text)
    p5=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(6)")
    a.append(p5.text)
    p6=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(7)")
    a.append(p6.text)
    p7=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(8)")
    a.append(p7.text)
    p8=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(9)")
    a.append(p8.text)

    p10=browser.find_element_by_css_selector("#js-screener-container > div.tv-screener__content-pane > table > tbody > tr:nth-child(1) > td:nth-child(11)")
    a.append(p10.text)
    return f"""Name of stock:{a[0]}
    Last price:{a[1]}
    Change %:{a[2]}
    Change value:{a[3]}
    Rating:{a[4]}
    Volume:{a[5]}
    Market Cap:{a[6]}
    Price to Earning Ratio:{a[7]}
    EPS(TTM):{a[8]}
    Sector:{a[9]}
    """

here is the output:这是 output:

Name of stock:RELIANCE INDUSTRIES LTD.
    Last price:2000.90
    Change %:0.79%
    Change value:15.60
    Rating:Buy
    Volume:210.603K
    Market Cap:12.656T
    Price to Earning Ratio:—
    EPS(TTM):—
    Sector:Energy Minerals

this is the expected output:这是预期的 output:

Name of stock:ZYDUS WELLNESS LTD.
        Last price:2110.15
        Change %:-0.57%
        Change value:-12.15
        Rating:Buy
        Volume:24.03K
        Market Cap:134.251B
        Price to Earning Ratio:114.58
        EPS(TTM):18.52
        Sector:Consumer Non-Durables

Now it is typing in the search box but giving wrong output which is actually the info of the stock at the homepage and not that of the search results.现在它在搜索框中输入错误 output 这实际上是主页上的股票信息而不是搜索结果。 Can anyone tell why it is happening?谁能告诉它为什么会发生?

Because you are using headless mode the send_keys won't work in this case.因为您使用的是headless模式,所以在这种情况下send_keys将不起作用。 See also this post .另见这篇文章 When you remove headless option and replace the "zydus" with eg "aapl" everything works fine.当您删除headless选项并将“zydus”替换为例如“aapl”时,一切正常。

One ways to work this around in your specific case would be eg:在您的特定情况下解决此问题的一种方法是:

from selenium.webdriver.common.keys import Keys

browser.execute_script("$('.tv-screener-table__search-input.js-search-input').val('{}');".format(aaa))
action = webdriver.ActionChains(browser);
action.send_keys(Keys.SPACE).send_keys(Keys.BACKSPACE).perform()

This works exactly the same way as entry.send_keys(aaa)这与entry.send_keys(aaa)的工作方式完全相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM