selenium python,尝试单击按钮

问题描述 投票:0回答:2

我正在尝试使用以下代码单击“加载更多”按钮

browser.find_element_by_xpath('//*[@id="mainContent"]/div[1]/div/div[5]/div/div[1]').click()
browser.find_element_by_css_selector('#mainContent > div.left-panel > div > div.result-list > div > div.content').click()
browser.find_element_by_link_text('Load More').click()

我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\Web_Scrap\setup.py", line 38, in <module>
    browser.find_element_by_css_selector('#mainContent > div.left-panel > div > div.result-list > div > div.content').click()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (239, 698)
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

我已经单独尝试了这3个中的每一个,但似乎无法获得硒来点击按钮

When pressing inspect that is the following element I receive

元素代码如下:

<div class="content" onclick="javascript:mtvn.btg.Controller.sendLinkEvent({ linkName:'PROFMIDPANE:LoadMore', linkType:'o' } );">Load More</div>

如果有人对如何实现这一点有任何建议,我将不胜感激!

更新:我尝试了向我推荐的两个解决方案,但遗憾的是没有成功,如果有人有兴趣,我会在这里发布。

iamsankalp89解决方案:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='content' and text()='Load More']")))
element.click()

错误信息:

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\Web_Scrap\setup.py", line 39, in <module>
    element.click()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (239, 698)
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

Julian Moreno解决方案:

ActionChains(driver).move_to_element("//div[@class='content' and text()='Load More']").click("//div[@class='content' and text()='Load More']").perform()

错误信息:

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\Web_Scrap\setup.py", line 42, in <module>
    ActionChains(browser).move_to_element("//div[@class='content' and text()='Load More").click("//div[@class='content' and text()='Load More").perform()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\action_chains.py", line 83, in perform
    action()
  File "C:\Users\David\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\action_chains.py", line 293, in <lambda>
    Command.MOVE_TO, {'element': to_element.id}))
AttributeError: 'str' object has no attribute 'id'
python-3.x selenium selenium-webdriver selenium-chromedriver
2个回答
0
投票

尝试使用ActionChains:

class selenium.webdriver.common.action_chains.ActionChains(driver)

ActionChains(driver).move_to_element(your_element).click(your_element).perform()

移动到元素会将鼠标(光标)移动到元素的中间(your_element),然后执行函数将执行链接的操作。

编辑

试试这个:

load_more = browser.find_element_by_css_selector("#mainContent > div.left-panel > div > div.result-list > div > div.content")
WebDriverWait(browser, timeout).until(EC.visibility_of(load_more))
browser.execute_script("return arguments[0].scrollIntoView(true);", load_more)
ActionChains(browser).move_to_element(load_more).click().perform()

ActionChains(browser).move_to_element()接受WebElement对象或String,它是元素的id。由于负载更多没有ID,因此move_to_element()找不到WebElement。

在分析了网页之后,我注意到move_to_element(load_more)移动到load_more按钮(直到它在屏幕内),但它也触发向下滚动并显示网页的页脚。此页脚覆盖了load_more按钮。因此,你需要browser.execute_script("return arguments[0].scrollIntoView(true);", load_more),它将基本上保持滚动,直到load_more在视图中(无论页脚显示)。


1
投票

试试这个xpath

//div[@class='content' and text()='Load More']

代码是这样的:

browser.find_element_by_xpath('//div[@class='content' and text()='Load More']').click()

也使用WebDriverWait

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='content' and text()='Load More']")))
element.click()
© www.soinside.com 2019 - 2024. All rights reserved.