繁体   English   中英

JavascriptException: Message: javascript error: arguments[0].click is not a function error using arguments[0].click through Selenium and Python

[英]JavascriptException: Message: javascript error: arguments[0].click is not a function error using arguments[0].click through Selenium and Python

我一直在对一些网站进行网络抓取,以获取那里进行网络抓取练习的位置。 这段代码让我了解酒店品牌的各个城市级别,但每当我在代码中使用 driver.execute_script("arguments[0].click();", button) 时(如倒数第二行所示),我得到这个错误:

JavascriptException: Message: javascript error: arguments[0].click is not a function

下面是我迄今为止编写的代码示例。

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)

此错误消息...

JavascriptException: Message: javascript error: arguments[0].click is not a function

...意味着使用execute_script() ) 在arguments[0]上调用click( ) 失败。


有关这些步骤的更多信息将有助于我们构建规范的答案。 但是,大概在您收集hotel_links之后:

driver.get(link2)
hotel_links = driver.find_elements_by_xpath('//div[@class="hotelList-detailsContainer"]//div//div//p//a')

所以最初, hotel_links包含WebElements 但在下一行中,您将使用elem.text覆盖List hotel_links ,如下所示:

hotel_links = [elem.text for elem in hotel_links]

因此, hotel_links现在包含text类型的元素。

由于文本元素不支持click() ,因此当您尝试通过execute_script()对文本元素调用click()时,您会看到上述错误。


解决方案

如果您需要酒店链接的文本,请将它们存储在单独的列表中,如下所示:

hotel_links_text = [elem.text for elem in hotel_links]

参考

您可以在以下位置找到一些相关的讨论:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM