Python Selenium - Select 单击下拉列表后
Python Selenium - Select after clicking dropdown
我想要 select 一个仅在您单击下拉菜单后才显示的选项(见附图)。我已经能够单击下拉列表以获取列表,但无法弄清楚如何单击选项,比如选项 1,列表出现后 'Last Day'。
这是我目前的情况:
from selenium import webdriver
binary = FirefoxBinary('C:\Program Files\Firefox Developer Edition\firefox.exe')
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True
url='https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=data+sc&sc.keyword=data+scientist&locT=C&locId=1154532&jobType='
driver = webdriver.Firefox(firefox_binary=binary, capabilities=cap, executable_path=GeckoDriverManager().install())
driver.get(url=url)
driver.implicitly_wait(10)
driver.maximize_window()
# clicking on dropdown
d = driver.find_element_by_id('filter_fromAge')
d.click()
我也尝试使用以下代码(在另一个 SO 答案中找到)但它也不起作用:
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"ul#css-1dv4b0s ew8xong0")))
我是网络抓取的新手,不太熟悉 XPATH 以及如何处理操作。感谢帮助!
您可以使用 Java 脚本来点击您的元素。由于元素存在于 DOM 中,但它仅在单击下拉列表时出现,因此正常的单击方法 mau 工作或可能不工作。但是对于 JS,它总是会点击。可以使用下面的代码:
day = driver.find_element_by_xpath("//span[contains(text(),'Last Day')]") #Identify your element
driver.execute_script("arguments[0].click();", day) # CLick it with help of JS
输出:
您可以尝试以下方法:
driver.get('https://www.glassdoor.com/Job/boston-data-scientist-jobs-SRCH_IL.0,6_IC1154532_KO7,21.htm')
driver.maximize_window()
expand_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'filter_fromAge')))
expand_element.click()
target_text = 'Last 3 Days'
target_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//ul[@class="css-1dv4b0s ew8xong0"]/li/span[text()="{}"]'.format(target_text))))
target_element.click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//div[@id="filter_fromAge"]/span[text()="{}"]'.format(target_text))))
到 select 文本为 最后一天 的选项仅在单击下拉列表后显示,您需要诱导 for the element_to_be_clickable()
and you can use the following :
使用XPATH
:
driver.get('https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=data+sc&sc.keyword=data+scientist&locT=C&locId=1154532&jobType=')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#filter_fromAge>span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='PrimaryDropdown']/ul//li//span[@class='label' and contains(., 'Last Day')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
我想要 select 一个仅在您单击下拉菜单后才显示的选项(见附图)。我已经能够单击下拉列表以获取列表,但无法弄清楚如何单击选项,比如选项 1,列表出现后 'Last Day'。
这是我目前的情况:
from selenium import webdriver
binary = FirefoxBinary('C:\Program Files\Firefox Developer Edition\firefox.exe')
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True
url='https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=data+sc&sc.keyword=data+scientist&locT=C&locId=1154532&jobType='
driver = webdriver.Firefox(firefox_binary=binary, capabilities=cap, executable_path=GeckoDriverManager().install())
driver.get(url=url)
driver.implicitly_wait(10)
driver.maximize_window()
# clicking on dropdown
d = driver.find_element_by_id('filter_fromAge')
d.click()
我也尝试使用以下代码(在另一个 SO 答案中找到)但它也不起作用:
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"ul#css-1dv4b0s ew8xong0")))
我是网络抓取的新手,不太熟悉 XPATH 以及如何处理操作。感谢帮助!
您可以使用 Java 脚本来点击您的元素。由于元素存在于 DOM 中,但它仅在单击下拉列表时出现,因此正常的单击方法 mau 工作或可能不工作。但是对于 JS,它总是会点击。可以使用下面的代码:
day = driver.find_element_by_xpath("//span[contains(text(),'Last Day')]") #Identify your element
driver.execute_script("arguments[0].click();", day) # CLick it with help of JS
输出:
您可以尝试以下方法:
driver.get('https://www.glassdoor.com/Job/boston-data-scientist-jobs-SRCH_IL.0,6_IC1154532_KO7,21.htm')
driver.maximize_window()
expand_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, 'filter_fromAge')))
expand_element.click()
target_text = 'Last 3 Days'
target_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//ul[@class="css-1dv4b0s ew8xong0"]/li/span[text()="{}"]'.format(target_text))))
target_element.click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//div[@id="filter_fromAge"]/span[text()="{}"]'.format(target_text))))
到 select 文本为 最后一天 的选项仅在单击下拉列表后显示,您需要诱导 element_to_be_clickable()
and you can use the following
使用
XPATH
:driver.get('https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=data+sc&sc.keyword=data+scientist&locT=C&locId=1154532&jobType=') WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#filter_fromAge>span"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='PrimaryDropdown']/ul//li//span[@class='label' and contains(., 'Last Day')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
浏览器快照: