Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium click on button xpath error

I am trying to scrape an airbnb listing. I cant figure out a way to get the full list of amenities other than clicking on "more". I am using selenium to simulate the click, but it does nt seem to work.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

url = 'https://www.airbnb.com/rooms/4660676'
driver = webdriver.Firefox()
driver.get(url)
elem = driver.find_element_by_xpath('//a[@class="expandable-trigger-more"]')
actions.click(elem).perform()
like image 208
Borat14 Avatar asked Jan 22 '16 16:01

Borat14


Video Answer


2 Answers

The XPath itself is correct, but you have not defined the actions:

from selenium.webdriver.common.action_chains import ActionChains

elem = driver.find_element_by_xpath('//a[@class="expandable-trigger-more"]')

actions = ActionChains(driver)
actions.click(elem).perform()

Note that you can simply use the WebElement's click() method instead:

elem = driver.find_element_by_xpath('//a[@class="expandable-trigger-more"]')
elem.click()

Works for me.


If you are getting NoSuchElementException error, you might need to wait for the link to be clickable via WebDriverWait and element_to_be_clickable.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//a[@class="expandable-trigger-more"]'))
)
element.click()
like image 131
alecxe Avatar answered Oct 17 '22 12:10

alecxe


A very simple way of achieving this is given below

driver.find_element_by_xpath('//a[@class="expandable-trigger-more"]').click()

It works for me hope will work for you as well.

like image 29
Mohsin Ashraf Avatar answered Oct 17 '22 11:10

Mohsin Ashraf