美文网首页
8-10(selenium-判断元素)

8-10(selenium-判断元素)

作者: 不知名的二狗 | 来源:发表于2019-08-26 16:05 被阅读0次

1.is_display

from common.base import Base
from selenium import webdriver
import os
driver = webdriver.Chrome()
driver.get("file:///"+os.path.abspath("login.html"))
login_test = Base(driver)
loc1 = ("name","user")
ele1 = login_test.findElement(loc1)

# 判断元素是否存在
r1 = ele1.is_displayed()
print(r1)

执行结果

True

2.is_selected

判断百度网页设置>>搜索设置>>每页显示条数下的option是否选中

from common.base import Base
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import os
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
select_test = Base(driver)
loc1 = ("link text","设置")
mouse = select_test.findElement(loc1)
ActionChains(driver).move_to_element(mouse).perform()

loc2 = ("link text","搜索设置")
select_test.click(loc2)
loc3 = ("xpath","//option[contains(text(),'50')]")
print(select_test.findElement(loc3).is_selected())

执行结果

False

3.radio

from common.base import Base
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import os
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
select_test = Base(driver)
loc1 = ("link text","设置")
mouse = select_test.findElement(loc1)
ActionChains(driver).move_to_element(mouse).perform()

loc2 = ("link text","搜索设置")
select_test.click(loc2)

loc3 = ("id","s1_1")
loc4 = ("id","s1_2")
r1 = select_test.is_Selected(loc3)
print(r1)
r2 = select_test.is_Selected(loc4)
print(r2)

执行结果

True
False

4.checkbox

checkbox.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
            <table border="1">
        <tr>
            <th><input type="checkbox" id = 'one' /></th>
            <th>$10</th>
        </tr>
        <tr>
            <td><input type="checkbox" id = "two"/></td>
            <td>$20</td>
        </tr>
        <tr>
            <td><input type="checkbox" id = "three"/></td>
            <td>$30</td>
        </tr>
    </table>
</body>
</html>

test_checkbox.py

from common.base import Base
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import os
driver = webdriver.Chrome()
driver.get("E:\workspace\pycharm_workspace\web_test\ke11\checkbox.html")
check_test = Base(driver)
loc1 = ("id","one")
r1 = check_test.is_Selected(loc1)
print(r1)
#点击
che1 = check_test.click(loc1)
r2 = check_test.is_Selected(loc1)
print(r2)

# 全部选中
loca_all = ("tag name","input")
all = check_test.findElements(loca_all)
print(all)
for i in all:
    if not i.is_selected():
        i.click()
print(r2)

执行结果

False
True

相关文章

网友评论

      本文标题:8-10(selenium-判断元素)

      本文链接:https://www.haomeiwen.com/subject/fcxdectx.html