尝试单击含Selenium的按钮时"Unable to locate element"或"TimeoutException"错误



我想点击"显示更多信息"在此清单的描述下使用selenium(我试图收集多个清单的数据)。

这就是HTML的样子

<button type="button" class="b1k5q1b3 v19vkvko dir dir-ltr">...</button>

我已经尝试使用By.CLASS_NAME方法,但我得到一个错误说无法找到元素:

url = "https://www.airbnb.com/rooms/50293998?adults=1&children=0&infants=0&check_in=2022-06-21&check_out=2022-06-28&federated_search_id=9f8562f3-653a-45b7-b6bd-218379131b41&source_impression_id=p3_1653519105_nsyk%2Fkxp2MNH1yam"
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver_path = "C:/Users/parkj/Downloads/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(service = Service(driver_path))
driver.get(url)
time.sleep(5)
button = driver.find_element(By.CLASS_NAME, "b1k5q1b3 v19vkvko dir dir-ltr")
button.click()

错误是这样的

如果我使用By.CSS_SELECTOR,我得到一个TimeoutException错误:

driver_path = "C:/Users/parkj/Downloads/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(service = Service(driver_path))
driver.get(url)
time.sleep(5)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "b1k5q1b3 v19vkvko dir dir-ltr")))

我仍然得到一个超时错误,即使我让驱动程序等待更长的时间。

from typing import Union
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.remote.webdriver import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, WebDriverException

def get_element(driver, by: By, identifier: str, timeout: int) -> Union[WebElement, None]:
try:
return WebDriverWait(driver, timeout).until(
EC.visibility_of_element_located((by, identifier))
)
except (WebDriverException, NoSuchElementException):
return None
driver = WebDriver(service = Service("path_to_driver"))
# The way to do it using class_name
button = get_element(By.CLASS_NAME, "b1k5q1b3 v19vkvko dir dir-ltr", 180)
# If you want to select by css
button = get_element(By.CSS_SELECTOR, ".b1k5q1b3 .v19vkvko .dir .dir-ltr", 180)

我个人在使用Selenium时总是使用这个函数。当按类选择时,你必须使用By.CLASS_NAME,如果你想使用CSS_SELCTOR,你必须在类名前面放一个.

TLDR:您缺少一个.

相关内容

最新更新