2016-06-30 29 views
0

我對Node.js完全陌生,目前正試圖將一些Python代碼重新寫入它。使用WebElements的NodeJS Selenium從findElements中返回

我在Python中有以下片段,其中driver是我的webdriver實例。

tabs = driver.find_elements_by_xpath('//div[@class="className"]') 
for tab in tabs:  
    print(tab.find_element_by_tag_name('span').text) 

我的Node.js這段代碼的解釋是

tabs = driver.findElements(webdriver.By.xpath('//div[@class="className"]'));   
for (var tab in tabs) {   
    console.log(tab.findElement(webdriver.By.css('span')).getText()); 
} 

但是我得到的錯誤

TypeError: tab.findElement is not a function

是什麼原因造成的?

我必須承認,我覺得我略微缺少Node.js的關鍵點,但也許這個例子可能會幫助我理解。

回答

0

如果我深入瞭解findElement()的硒源代碼,我發現它可能需要一個opt_root作爲參數。關於它的醫生說:

@param {?{ELEMENT: string}=} opt_root The WebElement reference for the * element to perform the search under. If not specified, will use * {@code document} for the target page.

所以我認爲與其tab.findElement(webdriver.By.css('span')你必須做的: driver.findElement(webdriver.By.css, 'span', tab)

注:我不確知如何node.js作品。我剛剛通過源代碼,所以我不確定它會工作。

相關問題