![](/img/trans.png)
[英]How do I click a Javascript button on a webpage using python selenium
[英]How to click on Javascript link in dynamic webpage using Selenium in Python?
我正在嘗試瀏覽這個動態網頁。 由於網站不是static,我只好用selenium。 在鏈接中,如果我們 go 到“目錄”選項卡,然后“選擇區域”,然后“選擇城市”,然后單擊出現的其中一個分支名稱,我們可以看到它的地址。
我想最終提取所有省份所有城市的所有地址並將它們復制到一個文件中; 但現在,我只是想 select 下拉列表中的第一個值,然后單擊其中一個地址。 到目前為止,這是我的代碼:
from selenium.webdriver.support.ui import Select
from selenium.webdriver import Chrome
import time
webdriver = r"path\to\chromedriver.exe"
driver = Chrome(webdriver)
driver.get('https://www.prulifeuk.com.ph/en/location-finder/')
# Click on the Directory tab
directory = driver.find_element_by_link_text("Directory")
directory.click()
time.sleep(1) # Wait 1 sec
'''
will write for loop later to loop through all territories
'''
# Select territory from dropdown
territory = Select(driver.find_element_by_id('territory'))
territory.select_by_index(2)
time.sleep(1)
'''
will write for loop later to loop through all cities in each territory
'''
# Selct city from dropdown
city = Select(driver.find_element_by_id('city'))
city.select_by_index(1)
time.sleep(1)
'''
will write for loop later to click through all offices in each city
'''
# Click on office
office = driver.find_element_by_xpath('//*[@id="map-list"]/a[1]/div/div[1]/h5')
office.click()
time.sleep(1)
該代碼最多可以從 2 個下拉列表中選擇值。 但是點擊辦公室的最后一個塊不起作用。 這是我得到的錯誤:
---------------------------------------------------------------------------
ElementClickInterceptedException Traceback (most recent call last)
<ipython-input-5-7c39aa1b8728> in <module>()
37 # Click on office
38 office = driver.find_element_by_xpath('//*[@id="map-list"]/a[1]/div/div[1]/h5')
---> 39 office.click()
40 time.sleep(1)
c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
631 params = {}
632 params['id'] = self._id
--> 633 return self._parent.execute(command, params)
634
635 def find_element(self, by=By.ID, value=None):
c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
c:\users\h473\appdata\local\programs\python\python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
ElementClickInterceptedException: Message: element click intercepted: Element <h5 class="iconLink d-block">...</h5> is not clickable at point (284, 515). Other element would receive the click: <div class="group-copy-cookie-bar">...</div>
(Session info: chrome=77.0.3865.120)
我怎樣才能讓它點擊動態鏈接來顯示地址文本?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.