driver.find_element 无法通过“class_name”找到元素

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

我尝试使用

driver.find_element
,通过“class_name”查找按钮并单击它以扩展-

上的房间
https://www.qantas.com/hotels/properties/18482?adults=2&checkIn=2024-04-16&checkOut=2024-04-17&children=0&infants=0#view-rooms

,但收到错误消息

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".css-v84xw-NakedButton eml2css7"}
  (Session info: chrome=123.0.6312.106); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception

带有按钮的 HTML 代码 -

<button data-testid="expand-offer-summary" aria-label="Expand offer details" type="button" class="css-v84xw-NakedButton eml2css7"><svg class="en7chz91 css-1osu69f-StyledSvg-Icon en7chz90" viewBox="0 0 24 24" title="expandMore" fill="currentcolor"><path d="M16.59 8.59L12 13.17 7.41 8.59 6 10l6 6 6-6z"></path></svg></button>

Python 代码(我尝试使用 explycity wait as wll -

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
def rooms():
    driver = webdriver.Chrome()
    driver.get("https://www.qantas.com/hotels/properties/18482?adults=2&checkIn=2024-04-16&checkOut=2024-04-17&children=0&infants=0#view-rooms")
    driver.implicitly_wait(5)
    content = driver.page_source
    soup = BeautifulSoup(content,'html.parser')
    Button = driver.find_element('class name',"css-v84xw-NakedButton eml2css7")
    Button.click()
    
rooms()
python html selenium-webdriver web-scraping
1个回答
0
投票

应该是这样的。

Button = driver.find_element(By.CSS_SELECTOR,".css-v84xw-NakedButton .eml2css7")
© www.soinside.com 2019 - 2024. All rights reserved.