admin管理员组

文章数量:1291036

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 = '/' + 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 = '/' + 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)

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./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./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)
Share Improve this question edited May 29, 2020 at 22:23 undetected Selenium 193k44 gold badges303 silver badges378 bronze badges asked May 29, 2020 at 20:05 rachelalllrachelalll 1222 gold badges2 silver badges5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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.


Solution

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]

Reference

You can find a couple of relevant discussions in:

  • What does arguments[0] and arguments[1] mean when using executeScript method from JavascriptExecutor interface through Selenium WebDriver?

本文标签: