1

I'm trying to select the checkboxes that are unchecked in this website: https://dataunodc.un.org/GSH_app , in 'National Data'. Moreover, I would like to select '100' from the Drop-Down Menu ('Show...entries').

I am using selenium and python. Could you tell me how to do it?

This is a part of the HTML code corresponding to the checkbox selection:

                    <label class="checkbox-inline">
                      <input type="checkbox" name="YearVar" value="1990" checked="checked">
                      <span>1990</span>
                    </label>
                    <label class="checkbox-inline">
                      <input type="checkbox" name="YearVar" value="1991">
                      <span>1991</span>
                    </label>
                    <label class="checkbox-inline">
                      <input type="checkbox" name="YearVar" value="1992"

And this one corresponds to the Drop-down menu:

 name="DataTables_Table_0_length" aria-
controls="DataTables_Table_0" class=""><option
 value="10">10</option><option
 value="25">25</option><option
 value="50">50</option><option 
 value="100">100</option></select> entries</label>
3
  • Do you have any code where you have tried to do this already? Post your attempts so people have something to build off and help you learn. Commented Dec 23, 2019 at 15:32
  • Post your code please Commented Dec 23, 2019 at 15:59
  • linked site is under maintenance. Please post full html Commented Dec 23, 2019 at 16:23

1 Answer 1

1

To get the all checkbox which are not checked and click on those checkbox.

Induce WebDriverWait() and visibility_of_all_elements_located() and following XPATH option.Then iterate the elements and click each of them.

#Get all checkbox which are not selected.
allchekbox=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//input[@name='YearVar' and not(@checked='checked')]")))

for item in allchekbox:
    item.click()

In order to select value for dropdown use selenium select class.

Induce WebDriverWait() and visibility_of_element_located() and either of Xpath option.

XPATH 1:

 # Select Item from dropdown
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//select[starts-with(@name,'DataTables_Table_')]")))
select = Select(element)
select.select_by_value("100")

Or

XPATH 2:

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//label[normalize-space(text())='Show']/select")))
select = Select(element)
select.select_by_value("100")

Here is the complete Code:

from selenium import webdriver
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
import time
driver = webdriver.Chrome()
driver.get("https://dataunodc.un.org/GSH_app")
driver.maximize_window()

#Switch the iframe in order to access the link
WebDriverWait(driver,15).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://unodc.shinyapps.io/GSH_App/']")))
#Click on National Data link
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,'//ul[@class="nav navbar-nav"]//a[text()="National Data"]'))).click()

#Get all checkbox which are not selected.
allchekbox=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//input[@name='YearVar' and not(@checked='checked')]")))

#iterate and click each checkbox
for item in allchekbox:
    item.click()

#To avoid StaleElementReferenceException add time.sleep()
time.sleep(2)
# Select Item from dropdown
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//select[starts-with(@name,'DataTables_Table_')]")))
select = Select(element)
select.select_by_value("100")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.