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!
Better wait for the frame explicitly:
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)
This would wait up to 10 seconds and then throw TimeoutException
. By default, it would check the presence of frame
every 500 ms.
Hope that helps.