【问题标题】:Selecting checkboxes using selenium web driver with python使用 selenium Web 驱动程序和 python 选择复选框
【发布时间】: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="{&quot;attribute&quot;:&quot;whitelist_categories&quot;,&quot;value&quot;:&quot;283155&quot;,&quot;rangeEnd&quot;:&quot;&quot;,&quot;rangeStart&quot;:&quot;&quot;,&quot;filterType&quot;:&quot;checkboxes&quot;}">

                <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


【解决方案1】:

我已经使用正确的 xpath 用一行非常简单的代码解决了我的问题:

self.browser.find_element_by_xpath("(//input[@name=''])[8]").click()

而且效果很好!

【讨论】:

  • 如果您尝试我建议的答案会发生什么?因为如果复选框元素会改变它们的位置索引,你的解决方案就不会更稳定..
  • 建议的两行代码都不能正常工作,对此我深表歉意,感谢您的合作。
【解决方案2】:

我已经修改了你的代码,现在它可以工作了

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()
        sleep(5)
        self.browser.find_element_by_xpath("//span[@class='a-label a-checkbox-label' and contains(text(),'Books')]/preceding-sibling::input[1]").click()
        self.browser.close()


if __name__ == "__main__":
    unittest.main(verbosity=2)

【讨论】:

  • 我已经检查了我的机器,它工作完美。你能给我你的配置吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2014-06-06
  • 1970-01-01
  • 2018-01-18
  • 2018-04-13
  • 1970-01-01
相关资源
最近更新 更多