[英]JavascriptException: Message: javascript error: arguments[0].click is not a function error using arguments[0].click through Selenium and Python
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:
这段代码让我了解酒店品牌的各个城市级别,但每当我在代码中使用 driver.execute_script("arguments[0].click();", button) 时(如倒数第二行所示),我得到这个错误:
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. ...意味着使用
execute_script()
) 在arguments[0]
上调用click( ) 失败。
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:但是,大概在您收集
hotel_links
之后:
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 .所以最初,
hotel_links
包含WebElements 。 But in the next line you are overwriting the List hotel_links
with the elem.text as follows:但在下一行中,您将使用elem.text覆盖List
hotel_links
,如下所示:
hotel_links = [elem.text for elem in hotel_links]
So, hotel_links
now contains elements of type text .因此,
hotel_links
现在包含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.由于文本元素不支持
click()
,因此当您尝试通过execute_script()
对文本元素调用click()
时,您会看到上述错误。
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:您可以在以下位置找到一些相关的讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.