为了使用 Selenium 自动化浏览器中的某些任务,我需要识别网站上的某个字段并单击它。因为我用来识别正确字段的值可以显示多次,所以我正在遍历包括多个条件的结果。也许代码编写得无效,但条件 - 以定位正确的 x 和 y 坐标为目标。我想知道我是否可以以某种方式修改 location['x'] 值以执行单击命令。 # finding the X Value tempmatchesx = driver.find_elements_by_xpath("//*[text()='" + tempoa + "']") tempmatchesxVal ='' if indicator == '1': for i in tempmatchesx: if (i.location['x'] >= temptype['x']) and (i.location['y'] >= temptype['y']) and (i.location['x'] < temptypeoppo['x']): tempmatchesxVal = i.location['x'] break elif indicator == '2': for i in tempmatchesx: if (i.location['x'] >= temptype['x']) and (i.location['y'] >= temptype['y']) and (i.location['x'] > temptypeoppo['x']): tempmatchesxVal = i.location['x'] break # finding the Y Value tempmatchesy = driver.find_elements_by_xpath("//*[text()='" + tempgoals + "']") tempmatchesyVal ='' if indicator == '1': for i in tempmatchesy: if (i.location['x'] >= temptype['x']) and (i.location['y'] >= temptype['y']) and (i.location['x'] < temptypeoppo['x']): i.location['x'] = tempmatchesxVal i.click() break elif indicator == '2': for i in tempmatchesy: if (i.location['x'] >= temptype['x']) and (i.location['y'] >= temptype['y']) and (i.location['x'] > temptypeoppo['x']): i.location['x'] = tempmatchesxVal i.click()所以基本上我的问题所指的部分如下:i.location['x'] = tempmatchesxVal i.click()在一次迭代中,是否有可能用之前确定的 x 值 (tempmatchesxVal) 替换 location-X 值?或者我的工作方式是否可行并且失败(没有错误代码)可能在其他地方?目前,没有任何项目被点击。更新:整体的目的是单击一个我现在不知道内容的元素,因此我不能简单地搜索它。在那里我确定了元素被涂敷的列和行。两个“find_elements_by_xpath”使用不同的输入完成 - 第一个是tempoa来识别列(x 值),第二个是行的tempgoals(y 值)。显然我无法修改 i.location[coordinate] - 然后如何单击该元素?
2 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
我已经自己解决了。我没有采用修改坐标的初始想法(感谢答案 - 所以这是不可能的),我实现了一个附加条件:
for i in tempOdds:
if (i.location['y'] == tempmatchesyVal) and (i.location['x'] >= tempmatchesxVal):
所以基本上只允许具有相同 x 和 y 坐标的元素/ x 可以更大,但第一个选择的是正确的
叮当猫咪
TA贡献1776条经验 获得超12个赞
我不完全清楚你的条件是什么,以及代码的一些目的是什么,但似乎非常错误的是为 i.location[something] 分配新值。 i是 html 元素,您可以获取它们的位置,但我认为设置它们不起作用。
更新:元素的位置是从页面左上角开始的像素位置。您提到了列和行:如果页面上有某种表格,我怀疑 i.location 是否可以帮助您识别要查找的列和行。但是,如果您喜欢使用像素偏移,则可以检查action_chains是否移动鼠标并单击。https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html
添加回答
举报
0/150
提交
取消
