Python Selenium Headless错误:元素不可交互

Python Selenium Headless错误:元素不可交互,python,selenium,selenium-chromedriver,Python,Selenium,Selenium Chromedriver,我正在制作一个程序,从使用selenium的网站上获取有关股票的信息。以下是代码: def库存(aaa): a=[] chrome_options=options() 自定义_头={'user-agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/74.0.3729.169 Safari/537.36'} chrome\u选项。添加\u参数(“--headless”) chrome

我正在制作一个程序,从使用selenium的网站上获取有关股票的信息。以下是代码:

def库存(aaa):
a=[]
chrome_options=options()
自定义_头={'user-agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,像Gecko)Chrome/74.0.3729.169 Safari/537.36'}
chrome\u选项。添加\u参数(“--headless”)
chrome_选项。添加_参数(f“user agent={custom_header}”)
chrome\u选项。添加\u参数(“--disable gpu”)
浏览器=webdriver.Chrome(选项=Chrome\u选项)
browser.get(“https://www.tradingview.com/screener/")
entry=browser.find\u element\u by\u css\u选择器("#js screener container>div.tv-data-table-sticky-wrapper.tv-screener-sticky-header-wrapper>table>thead>tr>th.tv-data-table\uu-cell.js-tv-data-table-th.js-tv-data-table-th-name.tv-data-table\uu-sortable.tv-screener-table\uu-sortable.tv-screener-table\uu-table\uu-draggable>div>div>div>query.tv-screener-table__search-query——无描述>输入)
输入。发送密钥(aaa)
当它没有处于headless模式时,它工作得很好,但当我切换到headless时,它给出了以下错误代码:

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)
以下是输出:

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
这是预期输出:

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
现在它在搜索框中输入信息,但输出错误,实际上是主页上的股票信息,而不是搜索结果。
有人能告诉我为什么会发生这种情况吗?

因为您使用的是
无头
模式,所以
发送键在这种情况下不起作用。
另见。
当您删除
headless
选项并将“zydus”替换为例如“aapl”时,一切正常

在您的具体案例中,解决此问题的一种方法是:

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()

这与
entry.send_key(aaa)的工作方式完全相同

旁注:
stock
不返回任何信息是打字错误吗?事实上,它会返回一些有关被搜索股票的信息,但我只给出了引起错误的部分代码,因为另一个代码非常长。还有其他发送键的选择吗?请参阅更新的答案现在问题是它正在将搜索输入到e输入但似乎不返回结果输出在编辑的帖子中这是一个不同的问题,不再适合原始帖子。要问新问题,请创建一个新帖子:每篇帖子一个问题。
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()