【问题标题】:Selenium Python - Nothing happens after click on elementSelenium Python - 单击元素后没有任何反应
【发布时间】:2021-12-09 21:47:07
【问题描述】:

我是 selenium 的新手,我尝试在页面上导航并单击按钮以使用 selenium Web 驱动程序进入下一页。

这是我的python代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[@id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]')))
    
driver.find_element(By.XPATH, '//*[@id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]').send_keys(Keys.RETURN)

print('Element found')

除了send_keys(Keys.RETURN),我还试过send_keys(Keys.ENTER)click()。在所有情况下,“找到元素”语句都会打印到控制台,但网页上没有任何反应。 这是按钮的 HTML 位:

<span class="ACME-src-ACME-ui-Pagination--arrowRight">
<i class="flaticon-right-arrow"></I>
</span>

我们非常感谢每一个帮助。

【问题讨论】:

  • 你试过用Javascript点击吗?

标签: python html selenium xpath


【解决方案1】:

等等!!看起来您使用的 Xpath 不正确。 Span 不是可点击的对象。或者就像它们是可点击的但不做任何事情,这正是在这里发生的。可能是“i”标签应该点击(但是我在 Dom 中看不到任何对象点击并执行工作)

首先为我提供正确的 DOM。

您可以进一步尝试另外两种方法:-

button = find_element(By.XPATH, '//*[@id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
driver.execute_script("arguments[0].click();", button)

button = find_element(By.XPATH, '//*[@id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
ActionChains(driver).move_to_element(button).click().perform()

【讨论】:

  • 非常感谢。 ActionChain 引导我找到答案,因为它抛出了“movetargetoutofboundsexception”。我向下滚动,现在 元素可以通过简单的 .click() 进行点击。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 2016-02-19
相关资源
最近更新 更多