2017-07-24 55 views
2

我有這行代碼:硒找到XPath的元素,然後單擊

browser=webdriver.Chrome() 
... 
games1=[] 
for x in browser.find_elements_by_xpath("//li[starts-with(@class,'icon_flag')]"): 
if x.text!="": 
    games1.append(x.get_attribute('class')) 

然後我試圖讓硒點擊我發現的元素:

for x in games1: 
browser.find_element_by_xpath("//li[@class=x]").click() 

怎麼可能得到錯誤消息:

消息:沒有這樣的元件:無法找到元素:

這真的很奇怪,因爲我發現網站的元素!

+0

添加一些console.log語句以縮小原因... – JGFMK

回答

5

您需要將x作爲變量傳遞。現在您正在傳遞字面值「x」值

for x in games1: 
browser.find_element_by_xpath("//li[@class="+x+"]").click()