Chrome Python selenium 元素不能相互作用

z4bn682m  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(98)

我试图运行这个基本的python selenium脚本,但我一直得到元素不可交互的错误。
我试着从By.Name切换到By.ID,甚至是By.XPATH,只是想看看是不是因为chromedriver无法识别NAME,但两者都不起作用

import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager

# set up chromedriver
service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

try:

    driver.get('https://www.google.com/')
    search_bar = driver.find_element(By.NAME, 'q')
    search_bar.send_keys("cats")
    
    button = driver.find_element(By.NAME, 'btnK')
    driver.implicitly_wait(2)
    button.click()
    # Click on the search bar to focus it
    ActionChains(driver).click(search_bar).perform()
    search_bar.send_keys(Keys.RETURN)

    time.sleep(5)

except Exception as e:
    print(f"an error occured: {e}")

finally:
    driver.quit()

我尝试了一个延迟,只是为了给予网站的时间来加载,以及一个重点的按钮。H

/usr/local/bin/python3.10 /Users/someone/Desktop/sel_tut/locating_elems.py 
an error occured: Message: element not interactable
  (Session info: chrome=117.0.5938.88)
Stacktrace:
0   chromedriver                        0x0000000104bbed58 chromedriver + 4336984
1   chromedriver                        0x0000000104bb6dd4 chromedriver + 4304340
2   chromedriver                        0x00000001047e38b4 chromedriver + 293044
3   chromedriver                        0x0000000104823078 chromedriver + 553080
4   chromedriver                        0x0000000104821a94 chromedriver + 547476
5   chromedriver                        0x000000010481e200 chromedriver + 532992
6   chromedriver                        0x0000000104863908 chromedriver + 817416
7   chromedriver                        0x000000010481ca5c chromedriver + 526940
8   chromedriver                        0x000000010481d908 chromedriver + 530696
9   chromedriver                        0x0000000104b84da4 chromedriver + 4099492
10  chromedriver                        0x0000000104b89260 chromedriver + 4117088
11  chromedriver                        0x0000000104b8f4ec chromedriver + 4142316
12  chromedriver                        0x0000000104b89d60 chromedriver + 4119904
13  chromedriver                        0x0000000104b61a34 chromedriver + 3955252
14  chromedriver                        0x0000000104ba6a08 chromedriver + 4237832
15  chromedriver                        0x0000000104ba6b84 chromedriver + 4238212
16  chromedriver                        0x0000000104bb6a4c chromedriver + 4303436
17  libsystem_pthread.dylib             0x0000000193c1bfa8 _pthread_start + 148
18  libsystem_pthread.dylib             0x0000000193c16da0 thread_start + 8

我试着在stackoverflow中测试类似问题的建议,到目前为止没有任何效果。非常感谢您提前为那些谁的React!

rvpgvaaj

rvpgvaaj1#

Google会默认隐藏搜索栏和按钮,并显示一个“继续Google之前”的弹出窗口,这就是为什么你不能访问一些dom元素的原因。这里有一个快速解决方法:

import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager

# set up chromedriver
service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

try:

    driver.get('https://www.google.com/')
    # accept eula
    accept_button = driver.find_element(By.ID, 'L2AGLb')
    accept_button.click()

    search_bar = driver.find_element(By.NAME, 'q')
    search_bar.send_keys("cats")
    
    button = driver.find_element(By.NAME, 'btnK')
    driver.implicitly_wait(2)
    button.click()
    # requery for search bar since page changed
    search_bar = driver.find_element(By.NAME, 'q')
    # Click on the search bar to focus it
    search_bar.click()
    search_bar.send_keys(Keys.RETURN)

    time.sleep(5)

except Exception as e:
    print(f"an error occured: {e}")

finally:
    driver.quit()

相关问题