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
1 Answer
Reset to default 7This 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?
本文标签:
版权声明:本文标题:JavascriptException: Message: javascript error: arguments[0].click is not a function error using arguments[0].click through Sele 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515285a2382848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论