Selenium(Python)不能在两个选择下拉菜单之间切换

时间:2018-07-07 12:02:56

标签: python jquery selenium web-scraping calendar

我目前正尝试使用Selenium,Python和Jupyter Notebook从该网站(https://www.sci.gov.in/judgments)上做出判断。

我目前无法选择jQuery日历中的日期。以下代码允许我更改月份:

driver.get("https://www.sci.gov.in/judgments")
driver.find_element_by_xpath('//*[@id="tabbed-nav"]/ul[2]/li[3]/a').click();
driver.find_element_by_xpath('//*[@id="JBJfrom_date"]').click();
selectByVisibleText = Select(driver.find_element_by_class_name('ui-datepicker-month'));
selectByVisibleText.select_by_visible_text("Jan");

添加以下代码应该可以更改年份:

selectByVisibleText2 = Select(driver.find_element_by_class_name('ui-datepicker-year'));
selectByVisibleText2.select_by_visible_text("1950");

但是,以上代码不起作用。我只是要更改年份或月份的下拉菜单,但不能同时更改两者。

我也无法让Selenium移至屏幕的任何其他部分。例如单击提交按钮。

driver.find_element_by_xpath('//*[@id="getJBJ"]').click();

为什么硒无法识别其他下拉列表,如何选择日期和月份?

非常感谢您的光临!

2 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.get("https://www.sci.gov.in/judgments")

wait = WebDriverWait(driver, 30) 

judgment_day = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Judgment Date')))
judgment_day.click()

calendar = wait.until(EC.element_to_be_clickable((By.ID, 'JBJfrom_date')))
calendar.click()

month = Select(driver.find_element_by_class_name('ui-datepicker-month'))
month.select_by_visible_text("Jan")

year = Select(driver.find_element_by_class_name('ui-datepicker-year'))
year.select_by_visible_text("1950")

date = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, '20')))
date.click()

submit_button = wait.until(EC.element_to_be_clickable((By.ID, 'getJBJ')))
submit_button.click()

答案 1 :(得分:0)

根据您提到的website,将 Month 选择为 Jan ,将 Year 选择为 1950 您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.select import Select
    
    options = Options()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe')
    driver.get('https://www.sci.gov.in/judgments')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Judgment Date"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='datepicker hasDatepicker' and @id='JBJfrom_date']"))).click()
    selectByVisibleText = Select(driver.find_element_by_xpath("//select[@class='ui-datepicker-month']"));
    selectByVisibleText.select_by_visible_text("Jan");
    selectByVisibleText2 = Select(driver.find_element_by_xpath("//select[@class='ui-datepicker-year']"));
    selectByVisibleText2.select_by_visible_text("1950");
    
  • 浏览器快照:

Jan_1950