元素与Selenium Python不相互作用

发布于 2025-02-12 04:08:09 字数 1027 浏览 0 评论 0原文

我知道这个问题在Stackoverflow上多次问过。我尝试了不同的解决方案,但没有使它起作用。这是一个简单的MWE,可以在YouTube上自动化搜索。任何熟悉此事的身体都可以帮助解释理由吗?

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


options = Options()
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--remote-debugging-port=9515')
options.add_argument('--disable-setuid-sandbox')
options.add_argument("--start-maximized")

driver = webdriver.Chrome(service=Service("/usr/bin/chromedriver"), options=options)

url = "https://www.youtube.com/"
driver.get(url)

search_area = driver.find_element(By.XPATH, '//*[@id="search"]')

driver.implicitly_wait(10)

search_area.send_keys('Lionel Messi', Keys.ENTER)


print(search_area.text)

I know that this question was asked many times on stackoverflow. I tried different solutions but did not get it work. Here is a simple MWE to automate the search on Youtube. Any body familiar with this can help explain the raison ?

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


options = Options()
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--remote-debugging-port=9515')
options.add_argument('--disable-setuid-sandbox')
options.add_argument("--start-maximized")

driver = webdriver.Chrome(service=Service("/usr/bin/chromedriver"), options=options)

url = "https://www.youtube.com/"
driver.get(url)

search_area = driver.find_element(By.XPATH, '//*[@id="search"]')

driver.implicitly_wait(10)

search_area.send_keys('Lionel Messi', Keys.ENTER)


print(search_area.text)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲性难收 2025-02-19 04:08:09

您的代码有2个问题:

  1. 您应该使用唯一的定位器。
    您在此处使用的定位器匹配该页面上的5个元素,因此Selenium返回您在需要第二个匹配时与您经过的定位器相匹配的页面上的第一个元素。
    该定位器将更好地工作//输入[@ID =“ search”]
  2. 您需要添加延迟。
    硒返回您search_area元素在页面上找到元素时,但仍未准备好与之交互。
    克服此问题的最好方法是使用明确的等待。
    因此,此代码应该更好地工作:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys


options = Options()
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--remote-debugging-port=9515')
options.add_argument('--disable-setuid-sandbox')
options.add_argument("--start-maximized")

driver = webdriver.Chrome(service=Service("/usr/bin/chromedriver"), options=options)
wait = WebDriverWait(driver, 20)

url = "https://www.youtube.com/"
driver.get(url)

search_area = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@id='search']")))

search_area.send_keys('Lionel Messi', Keys.ENTER)

print(search_area.text)

PS
driver.implicitly_wait(10)不是一个延迟命令,这不会将延迟10秒钟放在您的位置上。这仅定义了Selenium到元素存在的方式的超时。另外,您在定位元素后放置,这样就不会影响先前所在的元素。

There are 2 problems with your code:

  1. You should use unique locator.
    The locator you are using here matching 5 elements on that page, so Selenium returns you the first element on the page matching the locator you passing here while you need the second match.
    This locator will work better //input[@id="search"]
  2. You need to add a delay.
    Selenium returns you search_area element at the moment that element is found on the page while it is still may be not ready to be interacted with.
    The best way to overcome this issue is to use Explicit Waits.
    So, this code should work better:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys


options = Options()
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--remote-debugging-port=9515')
options.add_argument('--disable-setuid-sandbox')
options.add_argument("--start-maximized")

driver = webdriver.Chrome(service=Service("/usr/bin/chromedriver"), options=options)
wait = WebDriverWait(driver, 20)

url = "https://www.youtube.com/"
driver.get(url)

search_area = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@id='search']")))

search_area.send_keys('Lionel Messi', Keys.ENTER)

print(search_area.text)

P.S.
driver.implicitly_wait(10) is NOT a delay command, this will not put a delay of 10 seconds on the line where you put it. This only defines the timeout for Selenium to way for element presence. Also, you put if AFTER locating the element, so this could not affect the previously located element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文