如何正确单击网站上的按钮?

时间:2019-07-19 09:28:26

标签: python css selenium parsing selenium-webdriver

我正在尝试使用selenium和python解析网页,我需要单击页面末尾的按钮以加载更多链接。

我尝试通过find_element_by_css_selector进行此操作,但是这没有用。我也尝试过find_elements_by_xpath,但是我也遇到了一个问题

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

我希望这段代码可以加载其他链接。

2 个答案:

答案 0 :(得分:1)

使用Action ClassJavaScripts Executor单击按钮。

使用JavaScripts Executor

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

使用Action Class

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

对于Action类,您需要导入。

from selenium.webdriver import ActionChains

答案 1 :(得分:0)

我的期望是由于模式弹出窗口而导致您无法单击该按钮,该弹出窗口不允许单击其他任何东西:

enter image description here

所以我建议:

  1. Wait until "More" button is present
  2. Check if it is clickable
  3. If it is not clickable-假定存在模式弹出窗口,请关闭弹出窗口
  4. Click“更多”按钮

示例代码:

id="ToolsMenu"
相关问题