我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用selenium.webdriver.common.by.By.TAG_NAME。
def find_element(self, by=By.ID, value=None): if not By.is_valid(by) or not isinstance(value, str): raise InvalidSelectorException("Invalid locator values passed in") if self._w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})['value']
def find_elements(self, by=By.ID, value=None): if not By.is_valid(by) or not isinstance(value, str): raise InvalidSelectorException("Invalid locator values passed in") if self._w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self._execute(Command.FIND_CHILD_ELEMENTS, {"using": by, "value": value})['value']
def find_element(self, by=By.ID, value=None): """ 'Private' method used by the find_element_by_* methods. :Usage: Use the corresponding find_element_by_* instead of this. :rtype: WebElement """ if not By.is_valid(by) or not isinstance(value, str): raise InvalidSelectorException("Invalid locator values passed in") if self.w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self.execute(Command.FIND_ELEMENT, {'using': by, 'value': value})['value']
def find_element(self, by=By.ID, value=None): if self._w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self._execute(Command.FIND_CHILD_ELEMENT, {"using": by, "value": value})['value']
def find_elements(self, by=By.ID, value=None): if self._w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self._execute(Command.FIND_CHILD_ELEMENTS, {"using": by, "value": value})['value']
def find_element(self, by=By.ID, value=None): """ 'Private' method used by the find_element_by_* methods. :Usage: Use the corresponding find_element_by_* instead of this. :rtype: WebElement """ if self.w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self.execute(Command.FIND_ELEMENT, { 'using': by, 'value': value})['value']
def find_elements(self, by=By.ID, value=None): """ 'Private' method used by the find_elements_by_* methods. :Usage: Use the corresponding find_elements_by_* instead of this. :rtype: list of WebElement """ if self.w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self.execute(Command.FIND_ELEMENTS, { 'using': by, 'value': value})['value']
def test_find_flights(driver, open_blazedemo, from_port, to_port): # Find flight choose_departure_flight(driver, departure_flight=from_port) choose_arrival_flight(driver, arrival_flight=to_port) submit_form(driver) assert from_port in utils.get_text(driver, By.TAG_NAME, "h3") assert to_port in utils.get_text(driver, By.TAG_NAME, "h3") assert "reserve.php" in driver.current_url # Choose flight submit_form(driver) assert from_port in utils.get_text(driver, By.TAG_NAME, "h2") assert to_port in utils.get_text(driver, By.TAG_NAME, "h2") assert "purchase.php" in driver.current_url # Purchase flight submit_form(driver) assert "Thank you for your purchase today!" in utils.get_text(driver, By.TAG_NAME, "h1") assert "confirmation.php" in driver.current_url
def parse_content(self,url): try: self.driver.get(url) except Exception,e: print "give up one detail" return "" try: element = WebDriverWait(self.driver, 30).until( EC.presence_of_all_elements_located((By.TAG_NAME, 'table')) ) print 'element:\n', element except Exception, e: print Exception, ":", e print "wait failed" page_source = self.driver.page_source bs_obj = BeautifulSoup(page_source, "lxml") return '%s'%bs_obj.find('td', class_='a-content').p.get_text().encode('utf-8','ignore')
def test_simple_ip_search_should_return_result(selenium, base_url): """Tests a search for an IP address""" selenium.get('{}/'.format(base_url)) query = selenium.find_element_by_id('query') search_button = selenium.find_element_by_css_selector( "input.button[type='submit']") ipaddr = "192.168.42.42" query.send_keys(ipaddr) search_button.click() caption = WebDriverWait(selenium, 15).until( EC.text_to_be_present_in_element((By.TAG_NAME, "caption"), ipaddr) ) caption = selenium.find_element_by_tag_name('caption') assert ipaddr in caption.text
def wait_until_presence_of_element_located(self, how, what): """ Wait until a located element is present Parameters ---------- how: String How you want to locate the element. See selenium.webdriver.common.by what: String What you want to locate. Returns: -------- element: The element founded Example: -------- start_button = wait_until_presence_of_element_located( By.ID, "start-button") iframe = wait_until_presence_of_element_located(By.TAG_NAME, "iframe") """ return self.wait.until(EC.presence_of_element_located((how, what)))
def wait_until_text_inside_element_located(self, how, what, text): """ Wait until a located element contains some text Parameters ---------- how: String How you want to locate the element. See selenium.webdriver.common.by what: String What you want to locate. text: String The text which must be contained by the located element Returns: -------- element: The element founded Example: -------- conclusion_title = wait_until_text_inside_element_located( By.TAG_NAME, "h3", "Conclusion") """ return self.wait.until(EC.text_to_be_present_in_element((how, what), text))
def wait_until_visibility_of_element_located(self, how, what): """ Wait until a located element is visible, which means that it's rendered and the CSS display style is not "none". Parameters ---------- how: String How you want to locate the element. See selenium.webdriver.common.by what: String What you want to locate. Returns: -------- element: The element founded Example: -------- button = wait_until_visibility_of_element_located(By.TAG_NAME, "button") """ return self.wait.until(EC.visibility_of_element_located((how, what)))
def wait_until_invisibility_of_element_located(self, how, what): """ Wait until an element is invisible, which means that it's not rendered or the CSS display style is "none". Parameters ---------- how: String How you want to locate the element. See selenium.webdriver.common.by what: String What you want to locate. Returns: -------- element: The element founded Example: -------- button = wait_until_invisibility_of_element_located(By.TAG_NAME, "button") """ return self.wait.until(EC.invisibility_of_element_located((how, what)))
def wait_until_clickability_of_element_located(self, how, what): """ Wait until an element is clickable, which means that it's visible, enabled and nothing is above this element. Parameters ---------- how: String How you want to locate the element. See selenium.webdriver.common.by what: String What you want to locate. Returns: -------- element: The element founded Example: -------- button = wait_until_clickability_of_element_located(By.TAG_NAME, "button") button.click() """ return self.wait.until(EC.element_to_be_clickable((how, what)))
def type_text_in_element_located(self, how, what, text): """ Type text into an located element. Parameters ---------- how: String How you want to locate the element. See selenium.webdriver.common.by what: String What you want to locate. text: String The text that you want to type in the element Example: -------- type_text_in_element_located(By.TAG_NAME, "input", "Hello World!") """ element = self.wait_until_clickability_of_element_located(how, what) element.clear() element.send_keys(text)
def is_finished(self): self.wait.until(EC.frame_to_be_available_and_switch_to_it( (By.TAG_NAME, "iframe"))) self.wait.until(EC.visibility_of_element_located( (By.TAG_NAME, "iframe"))) # self.driver.implicitly_wait(10) time.sleep(2) self.driver.switch_to_default_content() vcode_path = './main.png' vcode_out_path = './main-cut.png' self.driver.save_screenshot(vcode_path) cut_vcode(vcode_path, vcode_out_path, 165, 265, 182, 274) RGB = get_color(vcode_out_path) if RGB == (110, 162, 47): return True elif RGB == (248, 179, 0): return False else: print(RGB) exit()
def linkedinrec_people(url): """ Get's the 10 "People Also Viewed" from a person's url """ time.sleep(2) driver.get(url) driver.implicitly_wait(15) try: wait = WebDriverWait(driver, 15) wait.until(EC.presence_of_element_located((By.CLASS_NAME, "right-rail"))) wait.until(EC.presence_of_element_located((By.TAG_NAME, "ul"))) wait.until(EC.presence_of_element_located((By.TAG_NAME, "li"))) a = driver.find_element_by_class_name('right-rail') cr = driver.find_element_by_class_name('core-rail') yr = cr.find_element_by_tag_name('section') xr = yr.find_elements_by_tag_name('div') ar = xr[5].text.split('\n') sr = ar[0] + ' ' + ar[1] y = a.find_element_by_tag_name('ul') x = y.find_elements_by_tag_name('li') title = [x[i].text.replace('\n', ' ') for i in range(len(x))] title.append(sr) link = [i.find_element_by_tag_name('a').get_attribute('href') for i in x] link.append(url) profile_detail = driver.find_element_by_class_name('profile-detail') summary = profile_detail.find_elements_by_class_name('pv-entity__summary-info') # experience = [i.find_element_by_tag_name('h3').text for i in summary] # education = profile_detail.find_element_by_class_name('pv-entity__degree-info').text.split('\n') # accomplishment = profile_detail.find_element_by_class_name('pv-accomplishments-block__content') # print(accomplishment.text) # interest = profile_detail.find_element_by_class_name('pv-deferred-area__content') # int_li = interest.find_elements_by_tag_name('li') # for i in int_li: # print(i.text) # return list(zip(title, link)) except: print('Cannot find it..') driver.quit()
def get_people_viewed(self, url): """ Parses's the Person's Name + Header and also parse's the 10 "People Also Viewed" from a person's url Returns list of tuples with (Name + Header, url) If people also viewed option not available, list will just be len(1) (Rarely, Selenium doesn't pick up the JS DOM, so the script tries 2 more times before raising an exception) """ self.driver.get(url) # count = 0 try: wait = WebDriverWait(self.driver, 20) wait.until(EC.presence_of_element_located((By.CLASS_NAME, "right-rail"))) # wait.until(EC.presence_of_element_located((By.TAG_NAME, "ul"))) # wait.until(EC.presence_of_element_located((By.TAG_NAME, "li"))) a = self.driver.find_element_by_class_name('right-rail') cr = self.driver.find_element_by_class_name('core-rail') y = a.find_element_by_tag_name('ul') x = y.find_elements_by_tag_name('li') link = [i.find_element_by_tag_name('a').get_attribute('href') for i in x] link.append(url) return link except: raise Exception('Tried and Cannot find it..') # self.driver.quit()
def find_element_by_tag_name(self, name): """Finds element within this element's children by tag name. :Args: - name - name of html tag (eg: h1, a, span) """ return self.find_element(by=By.TAG_NAME, value=name)
def find_elements_by_tag_name(self, name): """Finds a list of elements within this element's children by tag name. :Args: - name - name of html tag (eg: h1, a, span) """ return self.find_elements(by=By.TAG_NAME, value=name)
def find_element_by_tag_name(self, name): """ Finds an element by tag name. :Args: - name: The tag name of the element to find. :Usage: driver.find_element_by_tag_name('foo') """ return self.find_element(by=By.TAG_NAME, value=name)
def find_elements(self, by=By.ID, value=None): """ 'Private' method used by the find_elements_by_* methods. :Usage: Use the corresponding find_elements_by_* instead of this. :rtype: list of WebElement """ if not By.is_valid(by) or not isinstance(value, str): raise InvalidSelectorException("Invalid locator values passed in") if self.w3c: if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value return self.execute(Command.FIND_ELEMENTS, {'using': by, 'value': value})['value']
def options(self): """Returns a list of all options belonging to this select tag""" return self._el.find_elements(By.TAG_NAME, 'option')
def find_element_by_tag_name(self, name): return self.find_element(by=By.TAG_NAME, value=name)
def find_elements_by_tag_name(self, name): return self.find_elements(by=By.TAG_NAME, value=name)
def connect_to_webpage(self): url_target_sg = 'http://railway1.hinet.net/csearch.htm' url_target_gb = 'http://railway.hinet.net/ctkind2.htm' self.driver.delete_all_cookies() wait = WebDriverWait(self.driver, timeout=6) try: # Booking Single Ticket. if self.book_type == 1: self.driver.get(url_target_sg) wait.until( EC.presence_of_element_located( (By.TAG_NAME, 'button') ) ) # Booking Go-Back Ticket. elif self.book_type == 2: self.driver.get(url_target_gb) wait.until( EC.presence_of_element_located( (By.TAG_NAME, 'button') ) ) except: self.label_show_result.setText( '?????????????\n' + '???????????????????' )
def get(self, relative_url, ignore_errors=False): """ Tells the browser to navigate to a relative URL (e.g.: '/learners') and makes sure there were no console errors during page load. """ url = make_absolute_url(relative_url, self.live_server_url) self.driver.get(url) self.wait_until_loaded(By.TAG_NAME, 'body') if not ignore_errors: self.assert_no_console_errors() else: self.dump_console_logs()
def find_elements_by_tag_name(self, name): """ Finds elements by tag name. :Args: - name: The tag name the use when finding elements. :Usage: driver.find_elements_by_tag_name('foo') """ return self.find_elements(by=By.TAG_NAME, value=name)
def step_01_open_tested_page(selenium, base_url): selenium.get(base_url) # zkontrolujeme, ze na strance je napis <h2>Log In</h2> el = selenium.find_element_by_tag_name('h1') assert 'All products' in el.text # # ExplicitWait - kdyz se nahravaji veci pomoci JavaScriptu a nereloadne se cela stranka # WebDriverWait(selenium, 2).until( # EC.presence_of_element_located( # (By.TAG_NAME, 'h1') # ) # )
def step_02_click_on_login(selenium): el = selenium.find_element_by_link_text('Login or register') el.click() # zkontrolujeme, ze na strance je napis <h2>Log In</h2> el = selenium.find_element_by_tag_name('h2') assert 'Log In' in el.text # # ExplicitWait - kdyz se nahravaji veci pomoci JavaScriptu a nereloadne se cela stranka # WebDriverWait(selenium, 2).until( # EC.text_to_be_present_in_element( # (By.TAG_NAME, 'h2'), # 'Log In' # ) # )
def get_course_list(self, closed=False): """ Return a list of available courses. ToDo: go to a closed course """ print(self.current_url()) '''print( self.driver .find_element(By.ID, 'ox-react-root-container') .get_attribute('outerHTML') )''' self.wait.until( expect.visibility_of_element_located( (By.TAG_NAME, 'h1') ) ) courses = self.find_all( By.CSS_SELECTOR, '.my-courses-current-section .my-courses-item' ) if len(courses) == 0: print('No courses found: %s' % courses) return [] for position, course in enumerate(courses): print('%s : "%s"' % (position, course.get_attribute('data-title'))) return courses