【问题标题】:how to click with selenium in python onclick without class, id or name如何在没有类,ID或名称的情况下在python onclick中单击selenium
【发布时间】:2021-11-16 10:53:23
【问题描述】:

我正在尝试找到一种方法来单击“X”按钮,但我找不到单击此按钮的方法。

元素复制:

<div style="cursor: pointer; float:right; border-radius: 3px; background-color: red; font-size: 10px; color: white; height: 15px; width:15px; line-height: 15px;" onclick="fecharModal();">X</div>

Xpath:

//*[@id="meu_modal"]/div

CSS 选择器:

#meu_modal > div

试过了:

driver.find_element_by_css_selector("a[onclick*=fecharModal]").click();

进口:

从 selenium 导入 webdriver 从 selenium.webdriver.common.keys 从 selenium.webdriver.support.ui 导入密钥 selenium.webdriver.common.by import By from selenium.webdriver.support.ui 从导入 WebDriverWait selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions 从导入 NoSuchElementException selenium.webdriver.common.action_chains 导入 ActionChains 导入 时间enter code here

【问题讨论】:

  • 你试过driver.find_element_by_xpath("//*[@id="meu_modal"]/div").click()
  • 嗨@Huzaifa,是的,我已经尝试过你的建议和错误 - “NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”: "//*[@id='meu_modal']/div"}(会话信息:chrome=93.0.4577.82)
  • 我也试过了。 #tried1: WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='meu_modal']"))).click() #tried2: WebDriverWait(driver, 20) .until(EC.element_to_be_clickable((By.XPATH, "//div[@name='x']"))).click() #tried3: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By .XPATH, "//a[@class='onclick']"))).click()
  • 当您将该 xpath 放入 DOM 时,该元素是否会突出显示。如果是,请检查该元素是否在 iframe 中。
  • 标记是 div,但您尝试选择锚标记。

标签: javascript python html selenium


【解决方案1】:

假设您提供的 xpath 是准确的,我建议您尝试此代码,这可能对您有用。

yourEle = driver.find_element_by_xpath("//*[@id='meu_modal']/div")
driver.execute_script("arguments[0].click();", yourEle)

【讨论】:

  • 如果需要添加一些等待或线程睡眠。
  • 嗨@热心编码器,我试过你的建议,但不幸的是。没用。消息错误:“NoSuchElementException:消息:没有这样的元素:无法找到元素:”
【解决方案2】:

这似乎是一个基于角度的应用程序,主要是 WebDriverWait 应该完成这项工作,但仍然低于解决方案 illustarteSelenium 中单击 web 元素的所有方式。 代码试用 2 是更可取的最佳实践。

我将使用这个 xpath

//div[@onclick='fecharModal();' and text()='X']

代码试用 1:

time.sleep(5)
driver.find_element_by_xpath("//div[@onclick='fecharModal();' and text()='X']").click()

代码试用 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@onclick='fecharModal();' and text()='X']"))).click()

代码试用 3:

time.sleep(5)
button = driver.find_element_by_xpath("//div[@onclick='fecharModal();' and text()='X']")
driver.execute_script("arguments[0].click();", button)

代码试用 4:

time.sleep(5)
button = driver.find_element_by_xpath("//div[@onclick='fecharModal();' and text()='X']")
ActionChains(driver).move_to_element(button).click().perform()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

检查步骤:

Press F12 in Chrome -> 转到element 部分 -> 执行CTRL + F -> 然后粘贴xpath 看看,如果您想要的element 用@ 得到突出显示 987654336@匹配节点。

【讨论】:

  • 您好@Cruisepandey,感谢您抽出宝贵的时间进行解释。事实上仍然没有工作。代码 1:NoSuchElementException 代码 2:“TimeoutException:消息:” 代码 3:NoSuchElementException 代码 4:NoSuchElementException 我已按照您提到的有关 xpath 的“检查步骤”进行操作,结果以 1/1 突出显示。我认为这几乎就在那里。哈哈
  • @user16792636 :我很确定它是否在 iframe 中或不在 selenium 视图端口中。请检查 iframe,如果它在 iframe 中,请告诉我
猜你喜欢
  • 2020-02-29
  • 2018-09-04
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 2015-11-02
  • 2019-07-15
  • 1970-01-01
  • 2021-04-24
相关资源
最近更新 更多