使用Python 3.5&选中HTML页面中的复选框.硒 [英] Check a checkbox in an HTML page with Python 3.5 & Selenium
问题描述
我正在尝试选中使用Python 3.5和Selenium的HTML页面上显示的所有复选框.最终目标显然是要操纵它们,但我什至无法正确选择它们.
I am trying to select all checkboxes that appear on an HTML page with Python 3.5 and Selenium. The final goal is obviously to manipulate them, but I can't even manage to select them properly.
我不能只给您确切的URL,因为它是eBay上的一个特殊页面,您需要连接才能看到它.因此,以下是访问我感兴趣的网页的方法:
I can't just give you the exact URL, because it's a special page on eBay and you need to be connected to see it. So, here's how to access the web page that interest me:
- 转到 https://signin.ebay.co.uk/ws/eBayISAPI.dll
- 创建帐户或登录
- 转到 http://cgi5.ebay.co.uk/ws/eBayISAPI.dll?SellHub3 连接
- 键入"iPhone"或您想要的任何品牌,都没关系,然后单击开始销售"按钮.
- 您在页面上.
您会看到,如果键入"iPhone",eBay建议您从以下所有类别之一中进行选择:
You can see that, if you type 'iPhone', eBay proposes you to choose from one of all of these categories:
移动电话和交流
移动和智能手机
手机和PDA配件>其他手机配件
Mobile Phone & PDA Accessories > Other Mobile Phone Accessories
其他电话
智能手表
手机和PDA配件>电缆和放大器;适配器
Mobile Phone & PDA Accessories > Cables & Adapters
手机零件
手机和PDA配件>充电器和放大器船坞
Mobile Phone & PDA Accessories > Chargers & Docks
手机和PDA配件>耳机
Mobile Phone & PDA Accessories > Headsets
手机和PDA配件>保护套和保护套封面
Mobile Phone & PDA Accessories > Cases & Covers
批发与零售职位
其他批发及批发职位
无论页面上有多少个复选框,我的目标都是勾选第一个复选框.我只想检查第一个.就这么简单.在此示例中,这意味着选中移动和智能电话"复选框.
我尝试了XPATH方法:
I tried the XPATH method:
checkBox = driver.find_elements_by_xpath(".//input [@ type ='checkbox']")
checkBox = driver.find_elements_by_xpath(".//input[@type='checkbox']")
但是当我尝试print(checkBox)时,它返回[](=无).奇怪的是,在Firefox中,这个XPATH恰好满足了我的需求.
But when I try print(checkBox), it returns [ ] (=nothing). It's strange because in Firefox this XPATH give me just exactly what I need.
我还尝试使用班级名称选择它:
I also tried to select it with the class name:
checkBox = driver.find_elements_by_class_name('inpChkBox')
checkBox = driver.find_elements_by_class_name('inpChkBox')
但是再次,当我尝试打印它时什么也没有.
But again, it results in nothing when I try to print it.
如何仅生成页面上所有复选框的列表,以便以后对其进行操作?
How can I just generate a list of all the checkboxes on the page, in order to manipulate them after?
似乎所有复选框的ID,名称等都是唯一的,并且是自动生成的,因此我不能仅通过ID,值或名称来选择它们.我需要能够检测页面中所有复选框并将其放在列表中的东西.
感谢您的帮助
推荐答案
我也不确定该页面是否包含框架,但是我通常是这样做的.
I'm also not sure if that page has frames, but I usually do it this way.
driver.switch_to.default_content()
在我抓取元素之前.然后,我使用此函数查询元素.
before I scrape elements. Then I use this function to query the element.
def get_element(by_arg, by_method=By.ID):
return WebDriverWait(driver, MAX_TIMEOUT).until(
EC.presence_of_element_located((by_method, by_arg))
)
对于您的情况,应该是这样.
so for your case it would be.
check_box = get_element(".//input[@type='checkbox']", By.XPATH)
这篇关于使用Python 3.5&选中HTML页面中的复选框.硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!