मैं सेलेनियम के लिए फ़िथन में केवल कक्षा पहचानकर्ता के साथ तालिका की प्रतीक्षा करने के लिए फ़ंक्शन कैसे लिखूं? मेरे पास सेलेनियम के पायथन वेबड्राइवर फ़ंक्शंस का उपयोग करने के लिए सीखने का एक शैतान है।सेलेनियम प्रतीक्षाफोरमेंट
उत्तर
:
import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
with contextlib.closing(webdriver.Firefox()) as driver:
driver.get('http://www.google.com')
wait = ui.WebDriverWait(driver,10)
# Do not call `implicitly_wait` if using `WebDriverWait`.
# It magnifies the timeout.
# driver.implicitly_wait(10)
inputElement=driver.find_element_by_name('q')
inputElement.send_keys('Cheese!')
inputElement.submit()
print(driver.title)
wait.until(lambda driver: driver.title.lower().startswith('cheese!'))
print(driver.title)
# This raises
# selenium.common.exceptions.TimeoutException: Message: None
# after 10 seconds
wait.until(lambda driver: driver.find_element_by_id('someId'))
print(driver.title)
मैं बना दिया है अच्छा उपयोग करते हुए अनुभवों: - बस एक इंतज़ार
- time.sleep (सेकंड)
- webdriver.Firefox.implicitly_wait (सेकंड)
पहले एक बहुत स्पष्ट है कुछ सामान के लिए कुछ सेकंड।
मेरे सभी सेलेनियम स्क्रिप्ट्स (कुछ से लेकर 1 से 3 तक) नींद() से लेकर मेरे लैपटॉप पर चलाने पर काम करता है, लेकिन मेरे सर्वर पर प्रतीक्षा करने का समय एक विस्तृत सीमा है, इसलिए मैं implicitly_wait का उपयोग करता हूं() भी। मैं आमतौर पर implicitly_wait (30) का उपयोग करता हूं, जो वास्तव में पर्याप्त है।
एक अंतर्निहित इंतजार WebDriver बताने के लिए जब एक तत्व या तत्वों को खोजने के लिए है, तो वे तुरंत उपलब्ध नहीं हैं की कोशिश कर समय की एक निश्चित राशि के लिए डोम मतदान पर है। डिफ़ॉल्ट सेटिंग 0 है। सेट करने के बाद, अंतर्निहित प्रतीक्षा WebDriver ऑब्जेक्ट इंस्टेंस के जीवन के लिए सेट की गई है।
उचित XPath लोकेटर के साथ Wait Until Page Contains Element
का उपयोग करें।
<body>
<div id="myDiv">
<table class="myTable">
<!-- implementation -->
</table>
</div>
</body>
... आप निम्न कीवर्ड दर्ज कर सकते हैं:
Wait Until Page Contains Element //table[@class='myTable'] 5 seconds
जब तक मैं कुछ याद किया, वहाँ इस के लिए एक नया कार्य बनाने के लिए कोई जरूरत नहीं है उदाहरण के लिए, निम्न HTML दिया।
मामले में इस में मदद करता है ...
सेलेनियम आईडीई में, मैं जोड़ा ... कमान: waitForElementPresent लक्ष्य: // तालिका [@ class = 'PLN']
तो मैंने किया था फ़ाइल> निर्यात testcase को Python2 के रूप में (वेब चालक), और यह मुझे इस दिया ...
def test_sel(self):
driver = self.driver
for i in range(60):
try:
if self.is_element_present(By.XPATH, "//table[@class='pln']"): break
except: pass
time.sleep(1)
else: self.fail("time out")
मैं wait_for_condition के लिए अजगर के लिए निम्नलिखित कार्यान्वित के बाद से अजगर सेलेनियम चालक इस समारोह का समर्थन नहीं करता।
wait_for_condition("!Ext.Ajax.isLoading()")
JavaScript वैरिएबल सेट कर दिया जाता
wait_for_condition("CG.discovery != undefined;")
आदि:
def wait_for_condition(c):
for x in range(1,10):
print "Waiting for ajax: " + c
x = browser.execute_script("return " + c)
if(x):
return
time.sleep(1)
के रूप में प्रतीक्षा है कि एक ExtJS अजाक्स कॉल लंबित नहीं है प्रयोग की जाने वाली
वाहू! यह एक चैंप की तरह काम करता था: 'wait_for_condition ("$() सक्रिय == 0") ' – mattmc3
तुम हमेशा एक पाश में एक छोटी नींद उपयोग करें और यह अपने तत्व आईडी गुजारें सकता:
def wait_for_element(element):
count = 1
if(self.is_element_present(element)):
if(self.is_visible(element)):
return
else:
time.sleep(.1)
count = count + 1
else:
time.sleep(.1)
count = count + 1
if(count > 300):
print("Element %s not found" % element)
self.stop
#prevents infinite loop
यह ऐडेंस में है जो WebdriverWait करता है :) – erm3nda
सेलेनियम 2 के पायथन बाइंडिंग एक नया समर्थन वर्ग कहा जाता expected_conditions है एक तत्व दिखाई देने पर परीक्षण की तरह सभी प्रकार की चीजें करने के लिए .py। यह available here.
नोट है: ऊपर फ़ाइल अक्टू 12, 2012 तक ट्रंक में है, लेकिन नवीनतम डाउनलोड जो अभी भी 2.25 है में अभी नहीं। उस समय तक जब तक एक नया सेलेनियम संस्करण जारी नहीं किया जाता है, तब तक आप इस फ़ाइल को स्थानीय रूप से अभी सहेज सकते हैं और इसे अपने आयात में शामिल कर सकते हैं जैसे मैंने नीचे किया है।
जीवन को थोड़ा आसान बनाने के लिए, आप सेलेनियम wait until
तर्क के साथ इनमें से कुछ अपेक्षित हालत विधियों को जोड़ सकते हैं, जो सेलेनियम 1 में उपलब्ध कुछ समान काम करने के लिए तर्क देते हैं। उदाहरण के लिए, मैंने इसे अपनी बेस क्लास में रखा SeleniumTest बुलाया मेरी सेलेनियम परीक्षण कक्षाओं के सभी का विस्तार जो:
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import selenium.webdriver.support.expected_conditions as EC
import selenium.webdriver.support.ui as ui
@classmethod
def setUpClass(cls):
cls.selenium = WebDriver()
super(SeleniumTest, cls).setUpClass()
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(SeleniumTest, cls).tearDownClass()
# return True if element is visible within 2 seconds, otherwise False
def is_visible(self, locator, timeout=2):
try:
ui.WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))
return True
except TimeoutException:
return False
# return True if element is not visible within 2 seconds, otherwise False
def is_not_visible(self, locator, timeout=2):
try:
ui.WebDriverWait(driver, timeout).until_not(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))
return True
except TimeoutException:
return False
फिर आप इन आसानी से अपने परीक्षण में बहुत तरह उपयोग कर सकते हैं:
def test_search_no_city_entered_then_city_selected(self):
sel = self.selenium
sel.get('%s%s' % (self.live_server_url, '/'))
self.is_not_visible('#search-error')
उम्मीद है कि इस मदद करता है
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('www.url.com')
try:
wait = driver.WebDriverWait(driver,10).until(EC.presence_of_element_located(By.CLASS_NAME,'x'))
except:
pass
अगर मुझे सेलेनियम कमांड के बारे में कुछ पता नहीं है, तो मैं फ़ायरफ़ॉक्स के साथ सेलेनियम वेब विचार आरसी का उपयोग करता हूं। आप combobox में कमांड चुन सकते हैं और जोड़ सकते हैं और परीक्षण कोड को अलग-अलग भाषा निर्यात करने के बाद अपना टेस्ट केस समाप्त कर सकते हैं। जावा, रूबी, phyton, सी #, आदि ..
आसान समाधान की तरह:
from selenium.webdriver.common.by import By
import time
while len(driver.find_elements(By.ID, 'cs-paginate-next'))==0:
time.sleep(100)
आप तत्वों के सभी प्रकार के लिए इस समारोह को संशोधित कर सकते हैं। नीचे दिया गया एक वर्ग तत्व के लिए है:
जहां "ड्राइवर" ड्राइवर है, "element_name" वह क्लास नाम है जिसे आप ढूंढ रहे हैं, और "सेक" अधिकतम सेकंड्स है जिसे आप प्रतीक्षा करने के इच्छुक हैं।
def wait_for_class_element(driver,element_name,sec):
for i in range(sec):
try:
driver.find_element_by_class_name(element_name)
break
except:
print("not yet")
time.sleep(1)
कोई भी मौका आप सेलेनियम डॉक्स पीडीएफ के लिए यूआरएल अपडेट कर सकते हैं? ऐसा लगता है कि –
@ToranBillups: दुर्भाग्य से, यह अब आधिकारिक साइट पर नहीं दिखता है। यह एक प्रतिलिपि है जो मैं संदर्भित कर रहा था: http://scholar.harvard.edu/files/tcheng2/files/selenium_documentation_0.pdf। "WebDriverWait" के लिए खोजें। [ऑनलाइन दस्तावेज] (http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits) समान और शायद अधिक अद्यतित है। – unutbu
यह मेरे लिए निर्दोष काम करता है! धन्यवाद। – swdev