简体   繁体   中英

How to properly click a button on a site?

I am trying to parse through a web-page using selenium and python and I need to click a button in the end of the page to load more links.

I tried doing this via find_element_by_css_selector, however this didn't work. I also tried find_elements_by_xpath, however i ran into a problem as well

br.get('https://ru.armeniasputnik.am/economy/')
button=br.find_element_by_css_selector('.m-more')
button.click()

---------------------------------------------------------------------------
ElementClickInterceptedException          Traceback (most recent call last)
<ipython-input-57-efe3bd0e09ab> in <module>
      1 br.get('https://ru.armeniasputnik.am/economy/')
      2 button=br.find_element_by_css_selector('.m-more')
----> 3 button.click()

~/anaconda3/lib/python3.7/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):

~/anaconda3/lib/python3.7/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):

~/anaconda3/lib/python3.7/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))

~/anaconda3/lib/python3.7/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 <a href="/economy/?id=19596655&amp;date=20190713T183900" data-href="/economy/more.html?id=19596655&amp;date=20190713T183900" data-for="rubric-major" class="b-btn m-more">...</a> is not clickable at point (449, 692). Other element would receive the click: <div class="global-fade globalFade" style="display: block;"></div>
  (Session info: chrome=75.0.3770.142)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.1 x86_64)

br.get('https://ru.armeniasputnik.am/economy/')
python_button = br.find_elements_by_xpath("//*[contains(concat(@class), concat("m-more"))]")[0]
python_button.click()

  File "<ipython-input-56-81bfa73ca6e5>", line 2
    python_button = br.find_elements_by_xpath("//*[contains(concat(@class), concat("m-more"))]")[0]
                                                                                    ^
SyntaxError: invalid syntax

I expect this code to load additional links.

Use either Action Class or JavaScripts Executor to click on the button.

Using JavaScripts Executor

button=driver.find_element_by_css_selector('a.m-more')
driver.execute_script("arguments[0].click();",button)

Using Action Class

button=driver.find_element_by_css_selector('a.m-more')
ActionChains(driver).move_to_element(button).click(button).perform()

For Action class you need to imports.

from selenium.webdriver import ActionChains

My expectation is that you cannot click the button due to modal popup which doesn't allow anything else to be clicked:

在此处输入图片说明

So I would recommend to:

  1. Wait until "More" button is present
  2. Check if it is clickable
  3. If it is not clickable - assume that the modal popup is present, close the popup
  4. Click "More" button

Example code:

more = WebDriverWait(driver, 10).until(
    expected_conditions.presence_of_element_located((By.XPATH, "//a[contains(@class,'m-more')]")))
if not more.is_enabled():
    driver.find_element_by_xpath("//a[contains(@class,'modalClose')]").click()
more.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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