[英]Iterate through each item in accordion block with Selenium & Python
我有一個手風琴塊,我想單擊每個項目並截取屏幕截圖。 每個項目共享一個 class,所以我認為 for 循環可以工作,但我無法將其獲取到 select 項目。
HTML 結構:
<div class="accordionContainer">
<div class="accordion">
<h3>Click This</h3>
<div class="accordionContent" style="display:none">
</div>
<div>
<div class="accordion">
<h3>Click This</h3>
<div class="accordionContent" style="display:none">
</div>
<div>
</div>
Python:
detailsAccordion = browser.find_elements_by_class_name('accordion')
index = 1
for option in detailsAccordion:
option.click()
try:
element = ui.WebDriverWait(ff, 10).until(lambda driver : driver.find_element_by_xpath("//div[@class='accordion'][" + str(index) + "]/div[@class='accordionContent']").text != "" )
except:
print "Can't do it"
browser.quit()
index = index + 1
n = nextNumber(n)
browser.save_screenshot('{0}\{1}.png'.format(imagesPath, n))
option.click()
這導致超時並出現以下錯誤。 我看過這個錯誤,人們在使用 inte.net 選項/代理設置時遇到了麻煩——我沒有代理,所以不知道為什么會這樣;
[exec] Can't do it
[exec] Traceback (most recent call last):
[exec] File "viewEmployeeUseCase.py", line 82, in <module>
[exec] ff.save_screenshot('{0}\{1}.png'.format(imagesPath, n))
[exec] File "C:\Python26\lib\site-packages\selenium-2.20.0-py2.6.egg\selenium\webdriver\firefox\webdriver.py", line 75, in save_screenshot
[exec] png = RemoteWebDriver.execute(self, Command.SCREENSHOT)['value']
[exec] File "C:\Python26\lib\site-packages\selenium-2.20.0-py2.6.egg\selenium\webdriver\remote\webdriver.py", line 151, in execute
[exec] response = self.command_executor.execute(driver_command, params)
[exec] File "C:\Python26\lib\site-packages\selenium-2.20.0-py2.6.egg\selenium\webdriver\remote\remote_connection.py", line 280, in execute
[exec] return self._request(url, method=command_info[0], data=data)
[exec] File "C:\Python26\lib\site-packages\selenium-2.20.0-py2.6.egg\selenium\webdriver\remote\remote_connection.py", line 321, in _request
[exec] response = opener.open(request)
[exec] File "C:\Python26\lib\urllib2.py", line 391, in open
[exec] response = self._open(req, data)
[exec] File "C:\Python26\lib\urllib2.py", line 409, in _open
[exec] '_open', req)
[exec] File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
[exec] result = func(*args)
[exec] File "C:\Python26\lib\urllib2.py", line 1170, in http_open
[exec] return self.do_open(httplib.HTTPConnection, req)
[exec] File "C:\Python26\lib\urllib2.py", line 1145, in do_open
[exec] raise URLError(err)
[exec] urllib2.URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>
使事情變得簡單而不是等待內容填充工作正常並且我希望它通過以下方式完成;
for option in detailsAccordion:
#print option
option.click()
WebDriverWait(ff, 2)
n = nextNumber(n)
ff.save_screenshot('{0}\{1}.png'.format(imagesPath, n))
option.click()
我不認為隱式等待是你想要的,我不相信它在你的代碼中做任何事情。 “隱式等待是告訴 WebDriver 在嘗試查找一個或多個元素時,如果它們不是立即可用的,則在一定時間內輪詢 DOM。默認設置為 0。一旦設置,隱式等待就設置為生命周期WebDriver object 實例。” - 網絡驅動程序
您真正想要的是等待手風琴內容出現然后截取屏幕截圖的顯式等待。
抱歉,不是 Python 程序員,所以我在猜測確切的代碼。 但我認為你想要這樣的東西:
detailsAccordion = browser.find_elements_by_class_name('accordion')
for option in detailsAccordion:
option.click() # open div
#Wait until the accordionContent div has text
try:
element = WebDriverWait(browser, 10).until(lambda option : option.find_element_by_class_name("accordionContent").text != "" )
finally:
#Throw error cause the div didn't populate
browser.quit
n = nextNumber(n)
browser.save_screenshot('{0}\{1}.png'.format(imagesPath, n))
option.click() #close div
更新:抱歉,我認為我最初建議的解決方案存在兩個主要問題。 (1) 它應該是except:
而不是finally:
因為finally:
總是執行而不是僅在出現超時錯誤時執行。 (2) 與 Watir-Webdriver 不同,Selenium-Webdriver 似乎不允許檢查關於當前accordionContent
元素的accordion
。 最初提出的解決方案總是檢查頁面上的第一個accordionContent
(不好)。 我可以找到關於另一個元素的元素的唯一方法是使用 xpath(或 css-selector)。
以下內容已使用這兩個概念進行了更新:
detailsAccordion = browser.find_elements_by_class_name('accordion')
index = 1
for option in detailsAccordion:
print option
option.click()
try:
element = ui.WebDriverWait(browser, 10).until(lambda driver : driver.find_element_by_xpath("//div[@class='accordion'][" + str(index) + "]/div[@class='accordionContent']").text != "" )
except:
# Error if div didn't populate
print "Can't do it"
browser.quit()
index = index + 1
n = nextNumber(n)
browser.save_screenshot('{0}\{1}.png'.format(imagesPath, n))
option.click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.