Python 很难缩小复选框的XPath范围
我正在以下网站上练习硒元素: www.automationpractice.com 我有几个基本测试,我从下面开始:Python 很难缩小复选框的XPath范围,python,selenium,xpath,Python,Selenium,Xpath,我正在以下网站上练习硒元素: www.automationpractice.com 我有几个基本测试,我从下面开始: import unittest from webdriver import Driver from values import strings from pageobjects.homescreen import Homescreen class TestHomeScreen(unittest.TestCase): @classmethod def setUp
import unittest
from webdriver import Driver
from values import strings
from pageobjects.homescreen import Homescreen
class TestHomeScreen(unittest.TestCase):
@classmethod
def setUp(self):
self.driver = Driver()
self.driver.navigate(strings.base_url)
def test_home_screen_components(self):
home_screen = Homescreen(self.driver)
home_screen.logo_present()
def test_choose_dress(self):
home_screen = Homescreen(self.driver)
home_screen.choose_dress()
@classmethod
def tearDown(self):
self.driver.instance.quit()
从以下内容中读取这些测试:
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from values import strings
class Homescreen:
def __init__(self, driver):
self.driver = driver
def logo_present(self):
self.logo = WebDriverWait(self.driver.instance, 10).until(
EC.visibility_of_element_located((
By.ID, "header_logo")))
assert self.logo.is_displayed()
def choose_dress(self):
self.dresses = WebDriverWait(self.driver.instance, 5).until(
EC.visibility_of_element_located((
By.XPATH, '//*[@id="block_top_menu"]/ul/li[2]/a')))
self.dresses.click()
self.casual_dresses = WebDriverWait(self.driver.instance, 10).until(
EC.visibility_of_element_located((
By.XPATH,'//input[@type="checkbox" and @id="layered_category_9"]')))
测试主屏幕组件顺利通过,但测试选择失败。我已经缩小了范围,它在最后的XPATH上失败了,这是“休闲装”的复选框。找不到。我已在Chrome中确认此XPATH有效:
self.casual_dresses = WebDriverWait(self.driver.instance, 10).until(
EC.visibility_of_element_located((
By.XPATH,'//input[@type="checkbox" and @id="layered_category_9"]')))
在下页:
所以我不确定问题出在哪里。也许我遗漏了什么,因为它是嵌入的
我也知道我需要在我的代码中添加一些Try/Except,最终,我只是从这些东西开始
EC.visibility_of_element_located
与
要能够处理所需复选框,请将元素休闲装滚动到视图中,然后检查元素是否存在
driver.execute_script("arguments[0].scrollIntoView();", self.casual_dresses)
根据urlhttp://automationpractice.com/index.php?id_category=8&controller=category#/categories-当您试图调用元素上的单击()
时,您需要使用如下方法,而不是元素的可见性()
方法:
self.casual_dresses = WebDriverWait(self.driver.instance, 10).until(EC.element_to_be_clickable((By.XPATH,"//span[@class='checked']/input[@class='checkbox' and @id='layered_category_9']")))
非常感谢。我认为这是xpath本身的问题……我甚至没有考虑selenium调用本身。非常好,谢谢!这个解决方案也很有效,当我需要点击复选框时也会很好。
self.casual_dresses = WebDriverWait(self.driver.instance, 10).until(EC.element_to_be_clickable((By.XPATH,"//span[@class='checked']/input[@class='checkbox' and @id='layered_category_9']")))