简体   繁体   English

Python selenium 复选框元素单击

[英]Python selenium checkbox element click

Context: I'm writing a Python script to ultimately download postings on the following site ( https://ngc.taleo.net/careersection/ngc_pro/jobsearch.ftl?lang=en&_ga=2.59113261.1016801192.1577482911-1938918126.1574124672 ).上下文:我正在编写一个 Python 脚本来最终下载以下站点上的帖子( https://ngc.taleo.net/careersection/ngc_pro/jobsearch.ftl?lang=en&_ga=2.59113261.1016801192.1577482911-1938918141226 )。 I've been able to programmatically navigate the page to search for a keyword, click the search button and drop down a filter field for location.我已经能够以编程方式导航页面以搜索关键字,单击搜索按钮并下拉过滤器字段以查找位置。

Questions: I've been unable to write the logic to find the california checkbox under the location element.问题:我一直无法编写逻辑来找到 location 元素下的 california 复选框。 Can someone take a look at the page source snippet below and help me find the correct logic to click on the corresponding california location checkbox element?有人可以看看下面的页面源代码片段并帮助我找到正确的逻辑来单击相应的加利福尼亚位置复选框元素吗?

Here's a snippet of the page source surrounding the checkbox:以下是复选框周围的页面源代码片段:

<div id="fieldSetId-LOCATION-level-2" class="hidden-audible">State / Province</div><div id="LOCATION-level-2" class="filter-level-title">State / Province</div><input id="LOCATION-level-2-item-0" type="checkbox" name="2160420105" title="California" class="filter-checkbox"><div class="label-wrapper"><a><label for="LOCATION-level-2-item-0" class="checkbox-label"><div class="checkboxp checkbox-unchecked">&nbsp;</div><span class="valuesdiv"><span class="filter-text">California</span><span class="filter-quantity">(48)</span></span></label></a></div><input id="LOCATION-level-2-item-1" type="checkbox" name="2660420105" title="Maryland" class="filter-checkbox"><div class="label-wrapper"><a><label for="LOCATION-level-2-item-1" class="checkbox-label"><div class="checkboxp checkbox-unchecked">&nbsp;</div><span class="valuesdiv"><span class="filter-text">Maryland</span><span class="filter-quantity">(42)</span></span></label></a></div><input id="LOCATION-level-2-item-2" type="checkbox" name="2360420105" title="Virginia" class="filter-checkbox"><div class="label-wrapper"><a><label for="LOCATION-level-2-item-2" class="checkbox-label"><div class="checkboxp checkbox-unchecked">&nbsp;</div><span class="valuesdiv"><span class="filter-text">Virginia</span><span class="filter-quantity">(41)</span></span></label></a></div>

Here's a snippet of the Python script I have so far:这是迄今为止我拥有的 Python 脚本的片段:

from selenium import webdriver

#Navigate to the main job search page 
#https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca
browser = webdriver.Firefox(executable_path=r'C:\Users\etherealessence\AppData\Local\Programs\Python\Python36\geckodriver-v0.26.0-win64\geckodriver.exe')
browser.get(r'https://ngc.taleo.net/careersection/ngc_pro/jobsearch.ftl?lang=en&_ga=2.59113261.1016801192.1577482911-1938918126.1574124672')

#Fill in the "keyword" search input
keyword_element = browser.find_element_by_id('KEYWORD')
keyword_element.send_keys('SQL')

#Find search button and click
submit_button = browser.find_element_by_id('search')
submit_button.click()

#Drop down location field 
location_field = browser.find_element_by_id('LOCATION-link')
location_field.click()

Fyi this is my first time using selenium so please be kind.仅供参考,这是我第一次使用硒,所以请善待。

To click on the checkBox California .单击checkBox California You Induce WebDriverWait () and element_to_be_clickable () and following xpath .您 Induce WebDriverWait () 和element_to_be_clickable () 并遵循xpath

To click on location you need to scroll to element by using location_once_scrolled_into_view and then click on that.要单击位置,您需要使用location_once_scrolled_into_view滚动到元素,然后单击它。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver.get("https://ngc.taleo.net/careersection/ngc_pro/jobsearch.ftl?lang=en&_ga=2.59113261.1016801192.1577482911-1938918126.1574124672")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"KEYWORD"))).send_keys('SQL')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"search"))).click()
element=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@id='LOCATION-link' and text()='Location']")))
element.location_once_scrolled_into_view
element.click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[.//span[text()='California']]//div[@class='checkboxp checkbox-unchecked']"))).click()

Browser Snapshot:浏览器快照:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM