简体   繁体   English

如何使用Python中的Selenium在动态网页中点击Javascript链接?

[英]How to click on Javascript link in dynamic webpage using Selenium in Python?

I am trying to navigate this dynamic webpage .我正在尝试浏览这个动态网页 Since the website is not static, I have to use selenium.由于网站不是static,我只好用selenium。 In the link, if we go to the "Directory" tab, then "Select a Territory", then "Select a City", and then click on one of the branch names that show up, we can see its address.在链接中,如果我们 go 到“目录”选项卡,然后“选择区域”,然后“选择城市”,然后单击出现的其中一个分支名称,我们可以看到它的地址。

在此处输入图像描述

I want to eventually extract all addresses in all cities in all provinces and copy them into a file;我想最终提取所有省份所有城市的所有地址并将它们复制到一个文件中; but for now, I'm just trying to select the first values on the dropdowns and click on one of the addresses.但现在,我只是想 select 下拉列表中的第一个值,然后单击其中一个地址。 This is my code so far:到目前为止,这是我的代码:

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)

The code works upto selecting values from the 2 dropdowns.该代码最多可以从 2 个下拉列表中选择值。 But the last block to click on the office is not working.但是点击办公室的最后一个块不起作用。 This is the error I get:这是我得到的错误:

---------------------------------------------------------------------------
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)

How can I get it to click on the dynamic link to display the address text?我怎样才能让它点击动态链接来显示地址文本?

Maby try this?试试这个? CliCKy HeRe 点击这里

or或者

This aNd CliCky hErE这个和点击这里

havent tried selecting locations with google maps, you could always instruct it to select it with x-axis and y-axis.还没试过用谷歌地图选择位置,你总是可以用x轴和y轴指示它到select。 lAsT cLiCkY 最后点击

Lemme know whats the outcome:D让我知道结果是什么:D

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM