Python Selenium-在迭代中替换location[';x';]值,然后单击()
为了使用Selenium自动化浏览器中的某些任务,我需要在网站上标识某个字段并单击它。因为我用来识别正确字段的值可以多次显示,所以我在遍历包括多个条件在内的结果。也许代码写得不好,但条件——目标是定位正确的x和y坐标——正在发挥作用。我想知道是否可以修改location['x']值,以便执行click命令Python Selenium-在迭代中替换location[';x';]值,然后单击(),python,python-3.x,selenium,selenium-webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,为了使用Selenium自动化浏览器中的某些任务,我需要在网站上标识某个字段并单击它。因为我用来识别正确字段的值可以多次显示,所以我在遍历包括多个条件在内的结果。也许代码写得不好,但条件——目标是定位正确的x和y坐标——正在发挥作用。我想知道是否可以修改location['x']值,以便执行click命令 # finding the X Value tempmatchesx = driver.find_elements_by_xpath("//*[text()=
# 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()
#查找X值
tempmachesx=driver。通过xpath(“/*[text()=”+tempoa+“]”)查找元素
tempmatchesxVal=''
如果指示符='1':
对于我在tempmatchesx:
如果(i.location['x']>=TENTYPE['x'])和(i.location['y']>=TENTYPE['y'])以及(i.location['x']=TENTYPE['x'])和(i.location['y']>=TENTYPE['y'])以及(i.location['x']>TENTYPEOPPO['x']):
tempmatchesxVal=i.location['x']
打破
#求Y值
tempmatchesy=driver。通过xpath(“/*[text()=”“+tempgoals+”])查找元素
tempmatchesyVal=''
如果指示符='1':
对于我来说,在tempmatchesy:
如果(i.location['x']>=TENTYPE['x'])和(i.location['y']>=TENTYPE['y'])以及(i.location['x']=TENTYPE['x'])和(i.location['y']>=TENTYPE['y'])以及(i.location['x']>TENTYPEOPPO['x']):
i、 位置['x']=tempmatchesxVal
i、 单击()
因此,基本上我的问题所指的部分如下:
i.location['x']=tempmachesxval
i、 单击()
在迭代中,是否有可能用之前标识的X值(tempmatchesxVal)替换location-X值?
或者我的工作方式和失败(没有错误代码)可能在其他地方?
目前,没有单击任何项目
更新:
整体的目的是单击我现在不知道内容的元素,因此我不能简单地搜索它。在这里,我标识了元素所在的列和行
使用不同的输入完成两个“通过xpath查找元素”—第一个是tempoa,用于标识列(x值),第二个是tempgoals,用于标识行(y值)
显然,我无法修改I.location[坐标]-我如何才能单击该元素?我不完全清楚您的条件是什么,以及代码的某些目的是什么,但似乎非常错误的是将新值指定给I.location[某些东西]。
i
是html元素,您可以获取它们的位置,但我认为设置它们不起作用
更新:元素的位置是页面左上角以像素为单位的位置。您提到了列和行:如果页面上有某种表,我怀疑I.location是否适合您识别要查找的列和行。但是,如果您喜欢使用像素偏移,可以检查
动作链
以移动鼠标并单击 我自己解决了。与最初修改坐标的想法不同(由于答案-因此不可能),我实施了一个附加条件:
for i in tempOdds:
if (i.location['y'] == tempmatchesyVal) and (i.location['x'] >= tempmatchesxVal):
因此,基本上只允许具有相同x和y坐标/x的元素可以更大,但第一个选择的元素是正确的我需要做两次,因为搜索的元素不同。第一个是“tempoa”,第二个是“tempoa目标”。这样我就确定了某个列的位置以及所需的行。我不知道我要找的手机的内容,因此不可能进行其他搜索。有了know column和row,我认为所有需要的都在那里了,不,这只是选择(单击)元素坐标的问题:第一次搜索的x值和第二次搜索的y值。这有助于澄清吗?您不能使用Selenium设置坐标,只能使用JavaScript。你能分享URL或者至少是文本格式的HTML,以及页面截图,清楚地解释你的目标吗?