【问题标题】:Selenium (Python) does not switch between two select dropdown menusSelenium (Python) 不会在两个选择下拉菜单之间切换
【发布时间】:2018-07-07 12:02:56
【问题描述】:

我目前正在尝试使用 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();

为什么 selenium 无法识别另一个下拉菜单,我如何让它同时选择日期和月份?

非常感谢您的宝贵时间!

【问题讨论】:

  • 尝试在页面上执行javascript,例如driver.execute_script('document.getElementById("getJCN").click()')应该会让你点击提交按钮。并且月份选择器在页面上不可见
  • 感谢您的评论!原来上面的代码行driver.find_element_by_xpath('//*[@id="getJBJ"]').click(); 也可以工作。我设法通过将 chrome 驱动程序升级到最新版本来解决我的解决方案。我的代码现在可以工作并实现同时选择月份和日期。

标签: python jquery selenium web-scraping calendar


【解决方案1】:

你可以试试这个代码:

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()

【讨论】:

  • 感谢您的解决方案!我运行了上面的代码并遇到了无法选择日期和月份的相同问题。只有在我升级到最新版本的 chrome 驱动程序后,它才起作用。 :)
  • 是的!谢谢!
【解决方案2】:

根据您提到的 website 选择 Month 作为 JanYear 作为 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");
    
  • 浏览器快照:

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2022-11-11
    • 2018-02-17
    • 2018-08-06
    • 2023-04-09
    • 2022-01-23
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多