首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏元素阻止我单击按钮

隐藏元素阻止我单击按钮
EN

Stack Overflow用户
提问于 2020-07-31 13:49:35
回答 1查看 231关注 0票数 0

以下代码执行以下操作:

  1. 通过第一次日期
  2. 访问URL关闭“理解来源”弹出窗口,方法是单击“下载数据”按钮跳过
  3. 按钮
  4. 打开一个新选项卡,下一次日期

h 19>关闭前一个选项卡(第一次日期URL)<>H 210H 111再次尝试单击“下载数据”h 212g 213

我遇到的问题是6号,它给了我“元素点击拦截”错误,我想这是因为它认为出现了一个新的“理解起源”弹出窗口。

然而,这次没有弹出窗口出现,不像我第一次打开浏览器(第一次约会URL)。这次,我甚至尝试使用相同的代码单击“跳过”按钮,但它仍然给出了相同的错误。

当我打开一个新的选项卡时,我怎样才能绕过这个问题并点击“下载数据”按钮呢?注意:我打开了成千上万的标签,一张一张。

我的代码:

代码语言:javascript
复制
# Load Chrome driver and movement.uber.com/cities website
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)

# Attributing the city name and the center-most zone code (or origin) to variables so they can be inserted in the URL later
city = 'atlanta' # Note: Atlanta might be missing data from 10/25/2018 - 10/29/2018
origin_code = '1074'
coordinates = '&lat.=33.7489&lng.=-84.4234622&z.=12'

# Open URL for the first day in the desired city (change coordinates depending on city)
driver.get('https://movement.uber.com/explore/' + city + '/travel-times/query?si' + origin_code + '&ti=&ag=taz&dt[tpb]=ALL_DAY&dt[wd;]=1,2,3,4,5,6,7&dt[dr][sd]=' + 
           '2016-01-02' + '&dt[dr][ed]=' + '2016-01-02' + '&cd=&sa;=&sdn=' + coordinates + '&lang=en-US')

# Agree to privacy preferences
priv_pref_buton = driver.find_element_by_id('privacy_pref_optin')
priv_pref_buton.click()
        
# Skip button only shows up the first time you open the Chrome browser
time.sleep(6)
skip_button = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[3]/div/div[2]/div/div[2]/div/div[1]/button')
skip_button.click()

                                    
# Choosing correct data parameters (Traffix Analysis Zone) and opening date bar in preparation for the calendar loop
# Zone type dropdown only shows up the first time you open the Chrome browser
zone_type_dropdown = WebDriverWait(driver, 8).until(
    EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/div[1]/div[3]/div/div[2]/div/div/div/div/div[1]/div[2]/div')))
zone_type_dropdown.click()

traffic_analysis_zones = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[2]/div/div/div/div/div/ul/li[2]')))
traffic_analysis_zones.click()
        

# Buttons for downloading the dataset
download_button = driver.find_element_by_css_selector('div.f5 button') # //button[contains(@data-baseweb, "button")]')
download_button.click()
time.sleep(3)

# travel_times_download = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[3]/div/div[2]/div/div[1]/div/button[1]')
# travel_times_download.click()


# Day list and loop Solution proposed in StackOverflow
# Generating the correct URLs for each date
def getURL():
    date = datetime(2016,1,3)
    while date <= datetime(2020,3,31):
        yield ('https://movement.uber.com/explore/' + city + '/travel-times/query?si' + origin_code + '&ti=&ag=taz&dt[tpb]=ALL_DAY&dt[wd;]=1,2,3,4,5,6,7&dt[dr][sd]=' +
               date.strftime('%Y-%m-%d') + '&dt[dr][ed]=' + date.strftime('%Y-%m-%d') + '&cd=&sa;=&sdn=&lat.=33.7489&lng.=-84.4234622&z.=12&lang=en-US')
        date += timedelta(days=1)

# Perform iteration through URLs downloading the datasets for each URL
i = 0
print("urls: %i", len(list(getURL())))
for url in getURL():
    i += 1
    if i < 4:
        driver.execute_script("window.open('"+url+"', '_blank')")
        print(url)
        time.sleep(3)
        
        # Create function
        # Agree to privacy preferences
        try:
            priv_pref_buton = driver.find_element_by_id('privacy_pref_optin')
            priv_pref_buton.click()
        
        except:
            pass
        
        # Skip button only shows up the first time you open the Chrome browser
        try:
            time.sleep(6)
            skip_button = driver.find_element_by_css_selector('button.btn btn--link')
        
            skip_button.click()
            
        except:
            pass
        
        time.sleep(3)
        download_button = driver.find_element_by_css_selector('div.f5 button') # //button[contains(@data-baseweb, "button")]')
        download_button.click()
        time.sleep(1)
        
        # travel_times_download = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[3]/div/div[2]/div/div[1]/div/button[1]')
        # travel_times_download.click()
        
        # Switch to previous tab and close it (leaving us with the newly above opened tab)
        tabs = driver.window_handles
        
        if len(tabs) > 1:
            driver.switch_to.window(tabs[0])
            driver.close()
            driver.switch_to.window(tabs[1])

错误如下:

代码语言:javascript
复制
ElementClickInterceptedException          Traceback (most recent call last)
<ipython-input-89-80ae8e1decd5> in <module>
     70         time.sleep(3)
     71         download_button = driver.find_element_by_css_selector('div.f5 button') # //button[contains(@data-baseweb, "button")]')
---> 72         download_button.click()
     73         time.sleep(1)
     74 

c:\users\i539797\appdata\local\programs\python\python38-32\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\i539797\appdata\local\programs\python\python38-32\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\i539797\appdata\local\programs\python\python38-32\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\i539797\appdata\local\programs\python\python38-32\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 <button data-baseweb="button" class="by f6 ae ah f7 f8 f9 fa fb fc fd fe ff dj fg fh cx cy c6 fi fj fk bo bn bp bm b3 cb ck c0 fl fm fn fo fp fq fr fs ft fu fv fw fx fy f5 cd fz g0 g1">...</button> is not clickable at point (212, 634). Other element would receive the click: <div>...</div>
  (Session info: chrome=84.0.4147.105)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-31 15:22:00

问题是你的司机仍然专注于标签一。当您打开一个新选项卡时,您需要更改驱动程序以将焦点放在它上。

被截获的点击是由上一个选项卡上的下载弹出引起的。

选项1更改如下:

代码语言:javascript
复制
driver.execute_script("window.open('"+url+"', '_blank')")

对此:

代码语言:javascript
复制
driver.execute_script("window.open('"+url+"', '_blank')")
driver.switch_to_window(driver.window_handles[1])

选项2或者,不要使用选项卡。使用singe选项卡将execute_script更改为该选项卡有效:

代码语言:javascript
复制
driver.execute_script("window.open('"+url+"', '_self')")

实际上与:(也起作用)相同。

代码语言:javascript
复制
driver.get(url)

最终使用了,如果使用单个选项卡,则不需要这样做:

代码语言:javascript
复制
  # Switch to previous tab and close it (leaving us with the newly above opened tab)
        tabs = driver.window_handles
        
        if len(tabs) > 1:
            driver.switch_to.window(tabs[0])
            driver.close()
            driver.switch_to.window(tabs[1])

但是,由于选项卡现在总是1,所以无论如何都不应该运行这段代码。

如果标签是绝对的,一定要让我知道,我会再看看。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63193117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档