元素与Selenium Python不相互作用
我知道这个问题在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有2个问题:
您在此处使用的定位器匹配该页面上的5个元素,因此Selenium返回您在需要第二个匹配时与您经过的定位器相匹配的页面上的第一个元素。
该定位器将更好地工作
//输入[@ID =“ search”]
硒返回您
search_area
元素在页面上找到元素时,但仍未准备好与之交互。克服此问题的最好方法是使用明确的等待。
因此,此代码应该更好地工作:
PS
driver.implicitly_wait(10)
是不是一个延迟命令,这不会将延迟10秒钟放在您的位置上。这仅定义了Selenium到元素存在的方式的超时。另外,您在定位元素后放置,这样就不会影响先前所在的元素。There are 2 problems with your code:
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"]
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:
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.