简体   繁体   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

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()时,您会看到上述错误。


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:您可以在以下位置找到一些相关的讨论:

暂无
暂无

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

相关问题 JavascriptException:消息:javascript 错误:无法读取属性“单击”的未定义错误执行 JavaScript 到 Python ZC49DFB575F06FCBB50DFB575F06FCBB50DFB575CA - JavascriptException: Message: javascript error: Cannot read property 'click' of undefined error executing JavaScript through Python Selenium selenium.common.exceptions.WebDriverException:消息:未知错误:arguments [0] .click不是在Selenium Python中使用execute_script()的函数 - selenium.common.exceptions.WebDriverException: Message: unknown error: arguments[0].click is not a function using execute_script() in Selenium Python OpenQA.Selenium.WebDriverException:'未知错误:arguments[0].click 在尝试使用 ChromeDriver Selenium 单击 svg 元素时不是函数 - OpenQA.Selenium.WebDriverException: 'unknown error: arguments[0].click is not a function while trying to click svg element using ChromeDriver Selenium selenium.common.exceptions.JavascriptException:消息:javascript 错误:意外的标识符 - selenium.common.exceptions.JavascriptException: Message: javascript error: Unexpected identifier Seleneium 异常 - arguments[0].click 不是在 Selenium Python 中使用 execute_script() 的函数 - Seleneium exceptions - arguments[0].click is not a function using execute_script() in Selenium Python 错误selenium.common.exceptions.JavascriptException:消息:ReferenceError:未定义房间 - Error selenium.common.exceptions.JavascriptException: Message: ReferenceError: room is not defined ng-click和传递参数的语法错误 - syntax error with ng-click and passing arguments javascript错误:arguments [0] .setAttribute不是函数 - javascript error: arguments[0].setAttribute is not a function JavaScript-单击不是函数错误 - javascript - click is not a function error 获取参数[0] .setAttribute不是Selenium中的函数错误 - Getting arguments[0].setAttribute is not a function error in Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM