1

So recently tried to create a suite in python to run tests on some twitter share buttons. I used the "switch_to_frame" function to navigate into the iframe and select the button. Here is my code

class EntertainmentSocialMedia(CoreTest):
    def testEntertainmentTwitter(self):
        d = self.driver
        d.get(config.host_url + '/testurl')
        social_text = 'Follow @twitterhandle'
        print "Locating Entertainment vertical social share button"
        time.sleep(3)
        d.switch_to_frame(d.find_element_by_css_selector('#twitter-widget-0'))
        social_button = d.find_element_by_xpath('//*[@id="l"]').text
        self.assertTrue(str(social_text) in str(social_button))
        print social_button
        d.close()

My concern is that with multiple tests in the suite, sometimes selenium will timeout. Is there something wrong with my code or can it be improved? Ideally I'd like them to be as robust as possible and avoid timeouts. Thanks!

4

1 回答 1

1

最好明确地等待帧

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

...

d.get(config.host_url + '/testurl')

frame = WebDriverWait(d, 10).until(
    EC.presence_of_element_located((By.ID, "twitter-widget-0"))
)
d.switch_to_frame(frame)

这将等待最多 10 秒,然后抛出TimeoutException. 默认情况下,它将frame每 500 毫秒检查一次是否存在。

希望有帮助。

于 2014-06-23T18:15:42.110 回答