I tried to use driver.find_element
, by "class_name" to find button and click on it for expanding rooms on –
https://www.qantas.com/hotels/properties/18482?adults=2&checkIn=2024-04-16&checkOut=2024-04-17&children=0&infants=0#view-rooms
, but received error message
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 code with button –
<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 code(I tried with 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()
2
Answers
It should be like that. If there is only one class then use
find_element(By.CLASS_NAME," #what is class name")
. However, button has 2 different class so u need to useCSS_SELECTOR
insteadCLASS_NAME
. Finally fyi"class name"
doesn’t work in find_elements as an argument.For more details you can take look on Selenium Doc.
There are multiple ways to find the elements. In your case class name has 2 different values so it is better to use
CSS_SELECTOR
You can select byCLASS_NAME
if you want to pass only one word of a particular tag.buttons = driver.find_element(By.CSS_SELECTOR,"button.css-v84xw-NakedButton.eml2css7")
here
button
is the intended tag that we want to select andcss-v84xw-NakedButton.eml2css7
is class name so all values in the class are concatenated with a dot (.)The below code snippet selects all the rooms and expand those.