skip to Main Content

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


  1. 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 use CSS_SELECTOR instead CLASS_NAME. Finally fyi "class name" doesn’t work in find_elements as an argument.

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

    For more details you can take look on Selenium Doc.

    Login or Signup to reply.
  2. 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 by CLASS_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 and css-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.

    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
    import time
    
    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)
    
        # If you want to use xpath then uncomment below line
        # buttons = driver.find_elements(By.XPATH, "//button[contains(@data-testid,'expand-offer-summary')]")
        # # If you want to use classname with a single value then uncomment below line. I took the first value here as it is also returning same output
        # buttons = driver.find_element(By.CLASS_NAME,"css-v84xw-NakedButton")
        buttons = driver.find_element(By.CSS_SELECTOR,"button.css-v84xw-NakedButton.eml2css7")
        for btn in buttons:
            driver.execute_script("arguments[0].scrollIntoView();", btn)
            time.sleep(1)
            btn.click()
    
        # Taking page source at the end so that all the room details were expanded.
        content = driver.page_source
        soup = BeautifulSoup(content,'html.parser')
        driver.quit()
    
    rooms()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search