JavascriptException:消息:javascript错误:参数[0]。单击不是使用参数[0]的函数错误。请单击Selenium和Python
我一直在网上拉屎一些网站,以抓取有网站拉屎练习的位置。这段代码让我深入到酒店品牌的各个城市级别,但每当我使用driver.execute_脚本(“arguments[0]。click();”,button)在我的代码中(如倒数第二行所示),我都会遇到以下错误:JavascriptException:消息:javascript错误:参数[0]。单击不是使用参数[0]的函数错误。请单击Selenium和Python,javascript,python,selenium,selenium-webdriver,webdriver,Javascript,Python,Selenium,Selenium Webdriver,Webdriver,我一直在网上拉屎一些网站,以抓取有网站拉屎练习的位置。这段代码让我深入到酒店品牌的各个城市级别,但每当我使用driver.execute_脚本(“arguments[0]。click();”,button)在我的代码中(如倒数第二行所示),我都会遇到以下错误: JavascriptException: Message: javascript error: arguments[0].click is not a function 下面是我迄今为止编写的代码示例 for state in stat
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\u script()
对参数[0]
调用click()失败
关于这些步骤的更多信息将有助于我们构建一个规范的答案。但是,假设您正在收集
酒店链接
之后:
driver.get(link2)
hotel_links = driver.find_elements_by_xpath('//div[@class="hotelList-detailsContainer"]//div//div//p//a')
因此,最初,hotel_links
包含。但在下一行中,您将用以下elem.text覆盖列表hotel\u链接
:
hotel_links = [elem.text for elem in hotel_links]
因此,hotel\u links
现在包含类型为text的元素
由于文本元素不支持click()
,因此当您试图通过execute\u script()
对文本元素调用click()
时,您会看到上述错误
解决方案 如果您需要酒店链接的文本,请将其存储在单独的列表中,如下所示:
hotel_links_text = [elem.text for elem in hotel_links]
参考文献 您可以在以下内容中找到一些相关讨论: