上下文:我正在编写一个 Python 脚本来最终下载以下站点 ( https://ngc.taleo.net/careersection/ngc_pro/jobsearch.ftl?lang=en&_ga=2.59113261.1016801192.1577482911-1938918126.1574124672) 上的帖子。我已经能够以编程方式导航页面以搜索关键字,单击搜索按钮并下拉筛选字段以查找位置。
问题:我一直无法编写在位置元素下找到加利福尼亚复选框的逻辑。有人可以看看下面的页面源代码片段并帮助我找到正确的逻辑来单击相应的加利福尼亚位置复选框元素吗?
这是复选框周围的页面源代码片段:
<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"> </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"> </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"> </div><span class="valuesdiv"><span class="filter-text">Virginia</span><span class="filter-quantity">(41)</span></span></label></a></div>
这是我目前拥有的 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()
仅供引用,这是我第一次使用 Selenium ,所以请善待。
最佳答案
点击 checkBox
California 。您诱导 WebDriverWait
() 和 element_to_be_clickable
() 并遵循 xpath
。
要单击位置,您需要使用 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()
浏览器快照:
关于javascript - Python selenium 复选框元素单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59515695/