python + selenium get label text is null solution

First, determine whether the element is hidden

link = driver.find_element(*By_xx, 'value').is_displayed()
print(link)

If the output is False, indicating that the element is hidden.

Second, the solution

1, elements of a method to modify the current position (positioning elements modified embodiment, the positioning element or modify the path, etc.), using is_displayed () method of positioning element results to True.

  Since the definition of webdriver spec, selenium WebDriver only interact with the courseware elements, so get text information hidden element returns an empty string.

2, by obtaining text information element get_attribute () method.

  When obtaining the hidden text information elements, may be used get_attribute () method, obtained by textContent, innerText, innerHTML attributes.

  innerHTML returns inside the HTML element that contains all of the HTML tags.

  textContent obtained and innerText grayed text, without HTML tags. textContent is W3C-compliant text attributes, but IE does not support; innerText not specify the content of the W3C DOM, but FireFox does not support.

from selenium.webdriver.common.by import By
from test_case.common.home import Page


class CloudMainPage(Page):
    username_input = (By.ID, 'username')
    password_input = (By.ID, 'password')
    loging_button = (By.XPATH, '//*[@id="loginDiv"]/div[1]/div[1]/ul/li[4]/div[1]')
    loging_result = (By.XPATH, '//*[@id="tuichuxitong"]/span')

    def user_login(self, username, password):
        """
        用户登录
        :param username: 用户名
        :param password: 密码
        :return:
        """
        self.find_element(*self.username_input).clear()
        self.find_element(*self.username_input).send_keys(username)
        self.find_element(*self.password_input).clear()
        self.find_element(*self.password_input).send_keys(password)
        self.find_element(*self.loging_button).click()

    def login_result(self):
        return self.find_element(*self.loging_result).get_attribute('innerText')

 

Guess you like

Origin www.cnblogs.com/chen/p/10931424.html