python - 如何根据 Selenium、Python 和 Xpath 提供的 html 定位元素

标签 python selenium selenium-webdriver xpath webdriver

我正在使用 selenium 来填写网络表单,但由于某种原因这个特定的文本框给我带来了麻烦。它的 html 是:

<input type="text" class="black_ar" value="" size="25" id="type" name="type" 
onmouseover="showTip(this.id)" onmouseout="hideTip(this.id)" 
onblur="trimByAutoTag(this);onSubTypeChangeUnit('className',this,'unitSpan')" 
autocomplete="off" title="">

我用过:

driver.find_element_by_name('type').click() 

driver.find_element_by_xpath('xpath').click() 

但是他们都说找不到该元素。我不确定它是否在 iframe 中(如果有人知道检查方法),并且正在使用 chropath,它是一个 chrome 扩展来查找元素的 xpath。

编辑:chropath 在相对 xpath 下说“它可能是 svg/pseudo/comment/iframe 的子项。XPath 不支持它们。”如果有帮助的话

最佳答案

根据您共享的在文本框中单击HTML,您可以使用以下任一解决方案:

  • xpath:

    driver.find_element_by_xpath("//input[@class='black_ar' and @id='type']").click()
    
  • css_selector:

    driver.find_element_by_css_selector("input.black_ar#type").click()
    

关于python - 如何根据 Selenium、Python 和 Xpath 提供的 html 定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51994821/

相关文章:

java - 如何在 Selenium 中选择没有名称或 ID 的下拉菜单

python Selenium : Having trouble fixing NoSuchElementException for a specific button

python - 带有 matplotlib 的长垂直条形图

python - 使用 Python pandas 计算调整后的成本基础(股票买入/卖出的投资组合分析)

python - 加载并绘图 %Y-%m-%d %H :%M:%S from a file

java - Selenium 化物 Selenium StaleElementReferenceException : Element not found in the cache

angular - 如何在 Angular 2 中测试解析路径?

java - 即使我捕获它,也会抛出 NoSuchElementException 吗?

javascript - 从下拉元素中选择的选项不可见

python - 如何在让管理员在 Django 中默认工作的同时为我的应用程序创建自定义用户模型?