English 中文(简体)
每一件与Selenium和Alfonc
原标题:Iterate through each item in accordion block with Selenium & Python

我用一个旁观灯块,我要点击每个物品和营地;照亮。 每个项目都有一个类别,因此我认为休息会奏效,但我无法选择这些项目。

传真 结构:

<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>

粉碎

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()

这造成了以下错误。 我审视了这一错误和现象;人们在互联网选择/环境方面遇到麻烦——我没有代理,无法肯定为什么开始这样做;

 [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:Python26libsite-packagesselenium-2.20.0-py2.6.eggseleniumwebdriverfirefoxwebdriver.py", line 75, in save_screenshot
 [exec]     png = RemoteWebDriver.execute(self, Command.SCREENSHOT)[ value ]

 [exec]   File "C:Python26libsite-packagesselenium-2.20.0-py2.6.eggseleniumwebdriver
emotewebdriver.py", line 151, in execute
 [exec]     response = self.command_executor.execute(driver_command, params)

 [exec]   File "C:Python26libsite-packagesselenium-2.20.0-py2.6.eggseleniumwebdriver
emote
emote_connection.py", line 280, in execute
 [exec]     return self._request(url, method=command_info[0], data=data)
 [exec]   File "C:Python26libsite-packagesselenium-2.20.0-py2.6.eggseleniumwebdriver
emote
emote_connection.py", line 321, in _request
 [exec]     response = opener.open(request)
 [exec]   File "C:Python26liburllib2.py", line 391, in open
 [exec]     response = self._open(req, data)
 [exec]   File "C:Python26liburllib2.py", line 409, in _open
 [exec]      _open , req)
 [exec]   File "C:Python26liburllib2.py", line 369, in _call_chain
 [exec]     result = func(*args)
 [exec]   File "C:Python26liburllib2.py", line 1170, in http_open
 [exec]     return self.do_open(httplib.HTTPConnection, req)
 [exec]   File "C:Python26liburllib2.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()
问题回答

我认为,不言而喻的等待是你想要的,我不认为你正在做任何事情。 “默示的等待是告诉网络司机在试图找到一个要素或要素时,如果无法立即获得这些要素或要素,就将占用一定时间。 违约情况为0。 一经确定,网络司机物体的寿命即为默示等待。 - http://seleniumhq.org/docs/04_webdriver_advanced.html#implicit-waits”rel=“nofollow”>Webdriver

你们真正想要的是, 等待协议内容到场,然后接受屏幕。

Sorry, not a Python programmer, so I am guessing at the exact code. But I think you want something like:

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

UPDATE: Sorry, I think there are two major issues with the solution I originally suggested. (1) It should be except: instead of finally: since finally: always executes rather than just if there s a timeout error. (2) Unlike Watir-Webdriver, it seems that Selenium-Webdriver does not allow checking the accordionContent with respect to the current accordion element. The originally proposed solution is always checking the first accordionContent on the page (bad). The only way I could find the element with respect to another element is using xpath (or 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()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签