為了使用 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
提交
取消