【发布时间】:2016-10-11 12:57:13
【问题描述】:
我正在尝试使用带有 python 代码的 selenium Web 驱动程序对 Amazon.com 类别复选框执行测试,我尝试了几种方法,但我不确定如何选择 Book 类别的复选框,例如没有它的 id 或姓名。我使用了一些插件来获得正确的 xpath,比如用于 chrome 的 XPath Helper 和 Firefox 上的路径检查器......
这是我的代码,我的尝试已被注释。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from time import sleep
import unittest
class Test_PythonOrg(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
def tearDown(self):
sleep(4)
self.browser.close()
self.browser = None
def test_07_ListOfTodayDeals(self):
self.browser.get("http://www.amazon.com")
deals = self.browser.find_element_by_link_text("Today's Deals")
deals.click()
books = self.browser.find_element_by_id("widgetFilters")
books.find_element_by_xpath("/x:div[1]/x:div/x:span[8]/x:div/x:label/x:span").click()
#for i in range(20):
#try:
#self.browser.find_element_by_xpath(".//*[contains(text(), 'Books')]").click()
#break
#except NoSuchElementException as e:
#print('retry')
#time.sleep(1)
#else:
#print('Test Failed')
browser.close()
#sign_in_button = self.browser.find_element(By_XPATH,'(//input[@name=''])[8]')
#sign_in_button.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()
#sleep(5)
#self.assertTrue("No results found." not in self.browser.page_source)
if __name__ == "__main__":
unittest.main(verbosity=2)
HTML
<span class="a-declarative" data-action="gbfilter-checkbox" data-gbfilter-checkbox="{"attribute":"whitelist_categories","value":"283155","rangeEnd":"","rangeStart":"","filterType":"checkboxes"}">
<div class="a-checkbox checkbox checked a-spacing-micro"><label><input name="" value="" checked="" type="checkbox"><i class="a-icon a-icon-checkbox"></i><span class="a-label a-checkbox-label">
Books
</span></label></div>
</span>
【问题讨论】:
-
能否分享相关的 HTML
-
刚刚将 HTML 添加到帖子中
标签: python selenium xpath checkbox