【问题标题】:How to click on the element as per the html through Selenium and Python如何通过 Selenium 和 Python 根据 html 点击元素
【发布时间】:2018-08-18 23:23:19
【问题描述】:

我在让 python selenium 查找和单击链接时遇到问题。我正在尝试自动将学生 cmets 输入 D2L。 ICN_Feedback_3400653_125630 第一组数字是年级项目ID,第二组数字是学生ID。我的计划是使用 gspread 的 cmets 并通过 selenium 将它们粘贴到 d2l 中。但是,我无法单击弹出 java 脚本文本框的链接。

我尝试了以下不同的变体:

element = driver.find_elements_by_id('ICN_Feedback_3400653_125630')
element = river.find_element_by_xpath("""//*@id="ICN_Feedback_3400653_125630"]/d2l-icon""")
element = driver.find_element_by_css_selector("#ICN_Feedback_3400653_125630")
element.click()

find_element_by_id() 不起作用,因为页面上有 2 个 ID 实例,一个是我需要的链接,另一个是在做什么?。诚然,我对此并不精通。我会以错误的方式解决这个问题吗?

这是我从中提取的来源:

<div class="dco" id="z_ch" style="display:inline;float:right;"><div 
class="dco_c" style="display:inline;"><a class="d2l-imagelink" 
id="ICN_Feedback_3400653_125630" href="javascript:void(0);" 
onclick="return false;" title="Enter comments for FIRSTNAME LASTNAME in a 
new window" aria-label="Enter comments for FIRSTNAME LASTNAME in a new 
window" 
role="button"><d2l-icon icon="d2l-tier1:edit" class="x-scope d2l-icon- 
0"><svg viewBox="0 0 18 18" preserveAspectRatio="xMidYMid meet"                         
focusable="false" class="style-scope d2l-icon" style="pointer-events: 
none; display: block; width: 100%; height: 100%;"><g class="style-scope 
d2l-icon"><path d="M2.85 10.907l-.672 1.407L.033 17.26a.535.535 0 0 0 0 
.368.917.917 0 0 0 .155.184.917.917 0 0 0 .184.155A.54.54 0 0 0 .56 
18a.48.48 0 0 0 .18-.033l4.946-2.145 1.407-.672 8.53-8.53-4.244- 
4.243zM4.857 14l-1.515.657L4 13.143l.508-1.064 1.415 1.413zM16.707 
5.537l-4.244-4.244.707-.707a2 2 0 0 1 2.83 0L17.414 2a2 2 0 0 1 0 2.83z" 
class="style-scope d2l-icon"></path></g></svg></d2l-icon></a>
</div></div>

【问题讨论】:

  • @debanjanb 请停止向所有这些问题添加 [webdriver] 和 [selenium] 标签。 [webdriver] 指的是 W3C 规范并且是通用的。这个问题以及所有其他问题都专门针对 [selenium-webdriver] 标签所涵盖的 Selenium WebDriver。 [selenium] 也太通用了,与 [selenium-webdriver] 是多余的。我还删除了 [xpath],因为这不是关于 XPath 的问题……它碰巧提到了 XPath,但也提到了 CSS 选择器和 ID。我们不需要为问题中提到的每个主题添加标签。
  • @JeffC 我觉得你对标签有误解。个别志愿者一直在关注特定的标签。这些问题需要覆盖更广泛的受众,以获得经过充分研究的答案。不要打扰太多的标签编辑,而是专注于用你经过充分研究的答案来回答问题。
  • @DebanjanB 我非常了解标签。标签应该与问题相关。如果您将鼠标悬停在每个标签上,它将描述标签所涵盖的内容。您正在添加与问题无关的标签。我在之前的评论中解释过。请参阅tagging 了解更多信息。

标签: python selenium-webdriver


【解决方案1】:

根据您共享的 HTML,您可以在所需元素上调用 click(),您可以使用以下解决方案:

driver.find_element_by_xpath("//a[@class='d2l-imagelink'][starts-with(@id,'ICN_Feedback_')][contains(@title,'FIRSTNAME LASTNAME')]").click()

更新

正如您在讨论 Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click: 之后看到的错误 Element is not clickable at point 尝试使用 executeScript() 方法如下:

myElement = driver.find_element_by_xpath("//a[@class='d2l-imagelink'][starts-with(@id,'ICN_Feedback_')][contains(@title,'FIRSTNAME LASTNAME')]")
driver.execute_script("arguments[0].click();", myElement)

【讨论】:

  • 返回错误。有没有办法只单击出现的第一个元素消息:未知错误:元素 ... 在点 (894, 858) 不可点击)。其他元素会收到点击:
    ...跨度>
  • 对不起,我按回车换行...更新了上一条评论中的代码
  • 查看我更新的答案并让我知道状态
  • @CalvinHobbes 如果这个/任何答案对您/对您有帮助,对未来的读者有帮助,请为答案投票。
猜你喜欢
相关资源
最近更新 更多
热门标签