Selenium WebDriver 中的 JavaScript Executor 是什么?
它的用途是什么,我们如何在 Selenium WebDriver 中使用它?
一个例子将不胜感激
我一直在抓取一些网站以获取进行网页抓取练习的位置。这段代码让我了解了酒店品牌的各个城市级别,但是每当我在代码中使用 driver.execute_script("arguments[0].click();", button) 时(如倒数第二行所示),我得到这个错误:
JavascriptException: Message: javascript error: arguments[0].click is not a function
Run Code Online (Sandbox Code Playgroud)
下面是我迄今为止编写的代码示例。
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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试抓取我感兴趣的页面。为此,我需要从 HTML 中删除元素的属性。'style' 是我想要删除的。所以我从 Stackoverflow 中找到了一些代码。(我使用 Chrome 作为驱动程序)
element = driver.find_element_by_xpath("//select[@class='m-tcol-c' and @id='searchBy']")
driver.execute_script("arguments[0].removeAttribute('style')", element)
Run Code Online (Sandbox Code Playgroud)
代码中的arguments[0] 有什么作用?谁能具体解释arguments[0]的作用?
browser.execute_script("window.open('about:blank', 'tab2');")
browser.switch_to.window("tab2")
browser.get('http://bing.com')
Run Code Online (Sandbox Code Playgroud)
我在网上搜索在 python 中使用 selenium 打开新选项卡的方法,并且ctrl + 的方法t不适用于 chrome,所以我偶然发现了上面的一段代码,但是我无法理解 'excute_script' 的作用。