Selenium Python 脚本因未知原因失败

问题描述 投票:0回答:1

脚本的作用非常不言自明,但总而言之,它会打开 youtube 并在搜索栏中输入“hello”,然后按 Enter 键。问题是它打开了 youtube,但没有输入 hello 或按 Enter 键。

这是代码

#!/usr/bin/env python3

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

# Set up the driver for Chrome
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)

# Replace the URL below with the webpage you want to interact with
driver.get('https://youtube.com')

# Give the page some time to load
time.sleep(2)

# Locate the search bar using its name attribute
search_bar = driver.find_element(By.ID('search'))

# Words to type
words = ['hello']

# Type each word and press ENTER
for word in words:
    search_bar.clear()  # Clear the search bar before typing
    search_bar.send_keys(word)
    search_bar.send_keys(Keys.ENTER)
    time.sleep(3)  # Wait a bit between each word

# Close the browser when done
driver.quit()

我尝试将其切换到 Google.com 而不是 youtube,并且我检查了搜索的元素 ID,但我认为它都是正确的

python selenium-webdriver selenium-chromedriver
1个回答
0
投票

它不会因未知原因而失败。当您运行该脚本时,它会向您显示一条错误消息。这对你的问题会有帮助。错误消息的相关位是

search_bar = driver.find_element(By.ID('search'))
                                 ^^^^^^^^^^^^^^^
TypeError: 'str' object is not callable
  1. 第一个问题是你的定位器语法不是 python,它是 python 和 Java 或 C# 的混合。语法应该是

    search_bar = driver.find_element(By.ID, 'search')
    
  2. 第二个问题是您使用的ID在页面上不是唯一的。实际上有 3 个元素具有该 ID,而您要查找的元素是第二个,这意味着我们需要找到一个新的定位器来唯一标识我们想要的元素。下面的 CSS 选择器有效

    (By.CSS_SELECTOR, "ytd-searchbox #search")
    

使用这两个建议和更多的清理,工作脚本如下

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# Set up the driver for Chrome
driver = webdriver.Chrome()

# Replace the URL below with the webpage you want to interact with
driver.get('https://youtube.com')

wait = WebDriverWait(driver, 10)

# Words to type
words = ['hello', 'selenium']

# Type each word and press ENTER
for word in words:
    # Locate the search bar
    search_bar = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "ytd-searchbox #search")))
    search_bar.clear()  # Clear the search bar before typing
    search_bar.send_keys(word)
    search_bar.send_keys(Keys.ENTER)
    time.sleep(3)  # Wait a bit between each word

# Close the browser when done
driver.quit()


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# Set up the driver for Chrome
driver = webdriver.Chrome()

# Replace the URL below with the webpage you want to interact with
driver.get('https://youtube.com')

wait = WebDriverWait(driver, 10)

# Words to type
words = ['hello', 'selenium']

# Type each word and press ENTER
for word in words:
    # Locate the search bar
    search_bar = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "ytd-searchbox #search")))
    search_bar.clear()  # Clear the search bar before typing
    search_bar.send_keys(word)
    search_bar.send_keys(Keys.ENTER)
    time.sleep(3)  # Wait a bit between each word

# Close the browser when done
driver.quit()
© www.soinside.com 2019 - 2024. All rights reserved.