隐藏的元素阻止了我单击按钮

隐藏的元素阻止了我单击按钮

本文介绍了隐藏的元素阻止了我单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码执行以下操作:

The following code does the following:

  1. 访问具有第一个日期的URL
  2. 关闭了解起源"单击跳过"弹出窗口
  3. 点击下载数据"按钮
  4. 打开一个带有下一个日期的新标签
  5. 关闭上一个标签(第一个日期URL)
  6. 尝试点击下载数据"再次

我遇到的问题是数字6.错误,我认为这是因为它认为新的了解来源"弹出窗口出现.

The problem I have is on number 6. It gives me the "element click intercepted" error, and I assume it's because it thinks a NEW "Understanding origins" pop-up window appeared.

但是,这次没有弹出窗口,这与我第一次打开浏览器(第一个日期URL)不同.这次我什至尝试使用相同的代码单击跳过"按钮,但仍然会出现相同的错误.

However, no pop-up window appears this time, unlike the first time I opened the browser (FIRST date URL). I even tried using the same code to click on the SKIP button this time, but it still gives me the same error.

如何解决此问题,并能够单击下载数据"每次打开一个新标签页时都会显示一个按钮?注意:我要打开数千个标签,在这里一次打开一个标签.

How can I get around this and be able to click on the "Download data" button every time I open a new tab? Note: I'm opening thousands of tabs, one at a time here.

我的代码:

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

错误如下:

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)

推荐答案

问题是您的驱动程序仍然集中在选项卡一上.当您打开一个新标签时,您需要更改驱动程序以使其专注于此.

The problem was that your driver is still focussed on tab one.When you open a new tab you need to change the driver to focus to it.

被拦截的点击是由上一个选项卡上的下载弹出窗口引起的.

The intercepted click was caused by download popup on the previous tab.

选项1 更改此:

driver.execute_script("window.open('"+url+"', '_blank')")

对此:

driver.execute_script("window.open('"+url+"', '_blank')")
driver.switch_to_window(driver.window_handles[1])

选项 2另外,也不要使用标签.将 execute_script 更改为使用singe选项卡即可:

Option 2Alternatively, don't use tabs.Changing the execute_script to this use a singe tab works:

driver.execute_script("window.open('"+url+"', '_self')")

实际上与:(也可以)

driver.get(url)

最终如果使用单个标签,则不需要:

  # 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,因此无论如何都不应运行此代码.

however - since tabs will now always be 1, this code shouldn't run anyway.

如果制表符是绝对的,必须让我知道,我会再看一遍.

If tabs is an absolute must let me know and i'll have another look.

这篇关于隐藏的元素阻止了我单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 02:02