python - 单击我在 python 中用 selenium 找不到的按钮
问题描述
我有这个 html 元素:
<button class="expedition_button awesome-button " onclick="attack(null, '6', 2, 0, '')"><div></div><div></div></button>
并且由于某种原因我的代码无法单击它...只是给了我一个关于未按类查找元素的错误...
有没有办法发送按钮的功能“onclick”按钮持有的信息?即向该按钮的功能发送信息-“attack(null, '6', 2, 0, '')”
或使用此信息查找按钮
试过 xpath 但我似乎找不到它.. 只是给了我这个:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[2]/div/div[3]/div/div[2]/div[2]/div[2]/div[2]/div[2]/button
当我检查 xpath 时,我看到每次打开页面时它都会发生变化..
解决方案
好的,所以我尽可能地把它放在一起,因为我没有用 Python 编码,所以我的语法在某些地方可能不正确,但我试着做对了,在任何一种情况下,逻辑都在那里并且注释应该解释我在做什么:
首先让我们获取容器 div:
//Get the container which holds the 4 divs and their buttons
containerDiv = browser.find_element_by_id('expedition_list')
//Get the divs which contain the buttons
containerDivs = containerDiv.find_elements_by_class_name('expedition_box')
//Iterate the containers
for val in containerDivs:
//Get the div elements of the container
childDivs = val.find_elements_by_tag_name('div')
//iterate these divs, looking for the one which contains a button
for val1 in childDivs
buttonDiv = val1.find_elements_by_tag_name('button')
//Check to see if we found a div with a button
if len(buttonDiv) > 0
for button in buttonDiv
//If the button text is what we are looking for, click it
if button.Text = 'whatever your button's text is'
button.Click
推荐阅读
- python - 导出 TensorFlow 概率的隐马尔可夫模型
- php - 使用 Laravel eloquent 更新或创建数据透视表条目时避免唯一约束异常
- spring-boot - 微服务究竟是如何实现的?
- android - 从服务器 API 错误输出自定义 Rx java 中的 onError() 消息
- java - Spring boot docker容器返回文件而不是jsp视图
- swift - XCode 12/Swift 4 自定义单元格未显示在 UI 表格单元格视图上
- javascript - 如何从 MongoDB 中的嵌套数组中接收所有数据?
- react-native - 从不同导航器(React Native)的另一个屏幕返回时如何刷新屏幕?
- vue.js - Vue.js - 如何在数据对象更改时向父组件发出?
- python - 为什么我的 mod-mail 不起作用,但删除邮件在 discord.py Python 编程中起作用?