2016-03-26 42 views
5

注意:Selenium或API封裝的解決方案Splinter for Selenium很好!使用Splinter/Selenium與iFrames交互[Python]

我一直在使用Python的Splinter API與Twitter.com上的iframe進行交互。

例如,

with Browser('firefox', profile_preferences= proxySettings) as browser: 
    #...login and do other stuff here 
    browser.find_by_id('global-new-tweet-button').click() 

這帶來了一個彈出框鍵入在鳴叫。

如何與這個新的箱體採用分裂互動: 1)在消息 2)單擊「鳴叫」填寫(提交) ..programmatically當然。

我試着檢查元素,但它似乎並不嵌套在iframe中,但是它的目標是一個iframe。所以我不確定如何找到/與這個彈出窗口中的元素進行交互。

我試過手動在郵件中鍵入然後單擊鳴叫按鈕編程:

browser.find_by_css('.btn.primary-btn.tweet-action.tweet-btn.js-tweet-btn').click() 

..但我得到的錯誤:

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with 
Stacktrace: 
    at fxdriver.preconditions.visible (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/[email protected]/components/command-processor.js:10092) 
    at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/[email protected]/components/command-processor.js:12644) 
    at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/[email protected]/components/command-processor.js:12661) 
    at DelayedCommand.prototype.executeInternal_ (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/[email protected]/components/command-processor.js:12666) 
    at DelayedCommand.prototype.execute/< (file:///var/folders/z1/8rqrglqn2dj8_yj1z2fv5j700000gn/T/tmppRsJvd/extensions/[email protected]/components/command-processor.js:12608) 

我嚴格想用這樣分裂來實現我的目標請不要提供替代方案,我知道還有其他方法。 提前謝謝!

回答

0

您的主要問題似乎是您將browser.find_by_xxx的結果視爲元素對象,而實際上它是元素容器對象(即webdriver元素列表)。

寫入到外地工作對我來說,如果我引用明確的元素:

In [51]: elems = browser.find_by_id('tweet-box-global') 
In [52]: len(elems) 
Out[52]: 1 
In [53]: elems[0].fill("Splinter Example") 
In [54]: 

那會寫「分裂例」到現場爲我。

按鈕點擊失敗,因爲你的css路徑返回一個包含三個元素的列表,並且隱式地單擊第一個隱藏元素。在我的測試,你真正想要點擊的元素在列表中的第二個元素:

In [26]: elems = browser.find_by_css('.btn.primary-btn.tweet-action.tweet-btn.js-tweet-btn') 
In [27]: len(elems) 
Out[27]: 3 
In [28]: elems[1].click() 
In [29]: 

當我明確地點擊第二個元素,它不會引發錯誤,並單擊該按鈕。

如果您添加到您可以縮小結果只有按鈕可見模式的CSS路徑:

In [42]: css_path = "div.modal-tweet-form-container button.btn.primary-btn" 
In [43]: elems = browser.find_by_css(css_path) 
In [44]: len(elems) 
Out[44]: 1 
In [45]: elems.click() 
In [46]: 

注意,沒有出現異常拋出這裏。