I have been webscraping some websites to grab there locations for webscraping practice. This code gets me down to individual city levels of a hotel brand, but whenever I use driver.execute_script("arguments[0].click();", button) in my code (as seen in the second to last line) I get this error:
JavascriptException: Message: javascript error: arguments[0].click is not a function
Below is a sample of the code that I have written thus far.
for state in state_links:
driver = Chrome(path_to_chrome_driver)
link = 'https://www.ihg.com/destinations/us/en/united-states/' + state.lower().replace(' ', '-')
driver.get(link)
city_links = driver.find_elements_by_xpath('//div[@class="countryListingContainer col-xs-12"]//ul//div//li//a//span')
city_links = [thing.text for thing in city_links]
driver.close()
for city in city_links:
driver = Chrome(path_to_chrome_driver)
link2 = 'https://www.ihg.com/destinations/us/en/united-states/' + state.lower().replace(' hotels', '').replace(' ', '-') + '/' + city.lower().replace(' ', '-')
driver.get(link2)
hotel_links = driver.find_elements_by_xpath('//div[@class="hotelList-detailsContainer"]//div//div//p//a')
hotel_links = [elem.text for elem in hotel_links]
driver.close()
for hotel in hotel_links:
driver = Chrome(path_to_chrome_driver)
driver.get(link2)
driver.implicitly_wait(15)
driver.execute_script("arguments[0].click();", hotel)
driver.implicitly_wait(10)
This error message...
JavascriptException: Message: javascript error: arguments[0].click is not a function
...implies that invoking click() on arguments[0]
using execute_script()
failed.
A bit of more information about the steps would have helped us to construct a canonical answer. However, presumably as you are collecting the hotel_links
just after:
driver.get(link2)
hotel_links = driver.find_elements_by_xpath('//div[@class="hotelList-detailsContainer"]//div//div//p//a')
So initially, hotel_links
contains the WebElements . But in the next line you are overwriting the List hotel_links
with the elem.text as follows:
hotel_links = [elem.text for elem in hotel_links]
So, hotel_links
now contains elements of type text .
As text element doesn't support click()
, hence moving forward when you try to invoke click()
on the text elements through execute_script()
, you see the said error.
If you need the text of the hotel links, store them in a seperate List asfollows:
hotel_links_text = [elem.text for elem in hotel_links]
You can find a couple of relevant discussions in:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.