selenium--页面元素是否可见和可操作
判断元素是否可见
from selenium import webdriver import unittest class Test_Display(unittest.TestCase): def test_getDisplayed(self): url = 'http:www.baidu.com' self.driver = webdriver.Chrome() self.driver.get(url) div2 = self.driver.find_element_by_id('kw') # 判断元素是否在页面上可见 print(div2.is_displayed()) test1 = Test_Display() test1.test_getDisplayed()
结果:True
返回结果为True或False,True为可见
判断元素是否可操作
from selenium import webdriver import unittest class Test_ElementIsEnabled(unittest.TestCase): def test_getWebElementIsEnabled(self): url = 'D:\zouzou\hh.html' self.driver = webdriver.Chrome() self.driver.get(url) input1 = self.driver.find_element_by_id('input1') # 判断input1是否可操作 print(input1.is_enabled()) test1 = Test_ElementIsEnabled() test1.test_getWebElementIsEnabled()
结果为True或False,True是可操作的
判断元素是否已选中
from selenium import webdriver import unittest class Test_radio(unittest.TestCase): def test_SelectRadio(self): url = 'D:\zouzou\hh.html' self.driver = webdriver.Chrome() self.driver.get(url) berryRadio = self.driver.find_element_by_xpath('//input[@value="berry"]') berryRadio.click() # 断言该单选框是否被选中 self.assertTrue(berryRadio.is_selected(), '单选框未被选中') # 如果被选中,重新选择 if berryRadio.is_selected(): watermelonRadio = self.driver.find_element_by_xpath('//input[@value="watermelon"]') watermelonRadio.click() # 重新选择以后,断言刚才的选项处于未被选中状态 self.assertFalse(berryRadio.is_selected()) test1 = Test_radio() test1.test_SelectRadio()
is_enabled() 判断元素是否可操作
is_selected() 判断元素是否被选中