【问题标题】:How to click an image with Selenium in Python?如何在 Python 中使用 Selenium 单击图像?
【发布时间】:2021-03-26 18:43:39
【问题描述】:

我正在尝试单击此按钮,但该按钮实际上是一个没有 ID 的图像。我尝试过使用 Xpath 来做

WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "/html/body/table/tbody/tr[1]/td/a"))
).click()

但这无法找到该元素。任何帮助都会很棒!这是相关的 HTML

<a class="navcontent" href=f5-h-$$/MSL/jsp/openGimPage.jsp?gimEnv=GIM&amp;ep=GIMV" target="blank>
<img onclick="F5_r2u();F5_Event_common(event);try{return(eval(F5_Invoke_eval_event(null,F5_jsBody(function(){parent.trackUserActivity(this,'o','Portlet : Incident Management '+(this.src).split('/images/')[1].split('.')[0]);
<p id="text" style="top: 7px">View</p></a>

编辑:这是 HTML 的其余部分 Remainder of HTML

【问题讨论】:

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

尝试关注xpath 来识别元素。使用element_to_be_clickable() 而是presence_of_element_located()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[./p[text()='View']]"))).click()

【讨论】:

  • 文件 "C:\Users\tmartin\AppData\Local\Temp\ATMStatusScript.py1\main.py",第 22 行,在 WebDriverWait(driver, 10).until( File " C:\Users\tmartin\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) selenium。 common.exceptions.TimeoutException:消息:
  • 元素必须在iframe里面?你能检查Dom吗?如果它在那里,你需要切换它吗?
  • 抱歉,我该如何检查 Dom 以了解它是否在 iframe 中?
  • 其实我找到了。你知道如何引用它吗?
【解决方案2】:

找到了答案。正如 KunduK 所指出的,它在 iFrame 中。我必须使用以下内容选择 iFrame,然后单击其中的按钮。

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_css_selector("body > table > tbody > tr:nth-child(1) > td > a")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-19
    • 2018-09-22
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2017-12-26
    相关资源
    最近更新 更多