我试着用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代码(我try 使用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()

推荐答案

应该是这样的.如果只有一个类,则使用find_element(By.CLASS_NAME," #what is class name")个.然而,按钮有两个不同的类,所以你需要使用CSS_SELECTOR而不是CLASS_NAME.最后fyi "class name"在find_elements中不能作为参数工作.

Button = driver.find_element(By.CSS_SELECTOR,".css-v84xw-NakedButton .eml2css7")

更多细节可以看Selenium Doc.

Python相关问答推荐

try 与gemini-pro进行多轮聊天时出错

如何让剧作家等待Python中出现特定cookie(然后返回它)?

_repr_html_实现自定义__getattr_时未显示

Excel图表-使用openpyxl更改水平轴与Y轴相交的位置(Python)

如果值不存在,列表理解返回列表

Python解析整数格式说明符的规则?

为什么NumPy的向量化计算在将向量存储为类属性时较慢?'

将JSON对象转换为Dataframe

如何在表中添加重复的列?

删除marplotlib条形图上的底边

Django—cte给出:QuerySet对象没有属性with_cte''''

合并帧,但不按合并键排序

替换现有列名中的字符,而不创建新列

Numpyro AR(1)均值切换模型抽样不一致性

使用类型提示进行类型转换

SpaCy:Regex模式在基于规则的匹配器中不起作用

如果不使用. to_list()[0],我如何从一个pandas DataFrame中获取一个值?

删除另一个div中的特定div容器

如何批量训练样本大小为奇数的神经网络?

pyspark where子句可以在不存在的列上工作