0

I work with a CMS where I have to change a user status from "Pending" to "Confirmed". If I do it manually I have to click on a dropdown value. There are 12 dropdowns on the page the dropdown value is always the same. Then I have to refresh the browser and click through the next 12 dropdowns.

I wrote this Python script with Selenium:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options

import time

options = Options()
options.add_argument("user-data-dir=selenium")
driver = webdriver.Chrome(options=options)

driver.get("https://url.com")


select = Select(driver.find_element_by_class_name('address-status'))

select.select_by_value('1')

time.sleep(1)
driver.close()

This is the html for the dropdown:

<select name="data[status]" class="address-status" data-id="44980" id="status">
  <option value="2">Post card sent</option>
  <option value="1">Confirmed</option>
  <option value="0" selected="selected">Pending</option>
</select>

My problems which I can't figure out on my own are:

  1. The script only selects the first class of 'adress-status' and selects the value I want but then it stops. But there are 11 more identical classes/dropdown menus where the (same) value has to be selected.

  2. I don't know how to refresh the page to get the next set of dropdowns and restart the selection process/script.

I figured out that I may have to loop parts of the script but I learned and used Python for the first time yesterday and I just can't figure it out in time for this task.

ma66ot
  • 47
  • 9

3 Answers3

1

This is my solution

  1. Find all drop down element. It will return a list of all web element have class name = "address-status"

  2. Using loop to select confirmed value

     # find all dropdown element
     list_drop_down = driver.find_elements_by_class_name("address-status")
    
     # select confirmed value for each dropdown
     for drop_down_element in list_drop_down:
         select = Select(drop_down_element)
         select.select_by_value('1')
    
Trinh Phat
  • 524
  • 3
  • 6
  • Oh my god, this did the trick of selecting all 12 dropdown. This is like magic to me thanks :). Do you have an idea how close the browser and then restart the whole script again? I tried it with: import os | for i in range(x): | os.system("python script.py") but I get this error: ImportError: cannot import name 'webdriver' from 'selenium' (unknown location) – ma66ot Feb 14 '21 at 13:24
  • You can put all your code inside a loop instead of os.system("python script.py"). From the options to driver.close(). Put it all into loop. Or you can setting up PYTHONPATH. Following instruction from this link. https://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-so-it-finds-my-modules-packages. – Trinh Phat Feb 14 '21 at 14:10
  • If both not work. Create another question and post your code and error. It hard for me to visualize the problem with only a few lines. – Trinh Phat Feb 14 '21 at 14:13
  • Thanks man I will try your solution. You are awesome. I just accepted your solution. – ma66ot Feb 14 '21 at 14:44
  • My pleasure :)) – Trinh Phat Feb 14 '21 at 14:46
0

I had similar issue. You need to select address-status element by index.

  1. Find no of address-status elements
n_elements = len(driver.find_elements_by_class_name('address-status'))
  1. after each refresh find all the elements again, but choose the one at different index
for idx in range(n_elements):
    element = driver.find_elements_by_class_name('address-status')[idx]
    # do what you need with the element
    ...
    # refresh the page
    ...

Robert Axe
  • 396
  • 2
  • 11
  • Thanks! I'm not sure how to implement your code into mine. I just pasted your code after driver.get() but it doesn't do anything. It still just selects value of the first class and stops. Maybe the thing about the refreshing of the browser was a bit confusing of me. The process is: Launch URL in Browser - Select one dropdown and value ('1') after another for 12 times - then close browser with driver.close() - restart the script – ma66ot Feb 14 '21 at 13:18
  • Of course it does not. You need to add your code which are the functionality what you do with those elements. I believe that select only select the element, but does not click. You need to click on it. By the way, why do you want to refresh it each time? – Robert Axe Feb 14 '21 at 13:24
  • Hi Robert, sorry for not being able to apply your solution. My very little experience makes it a bit hard to understand your code but I'm sure my problem can also be solved by it. The solution of Phat Trinh helped me. In fact there is no need to click on the select option value. I tried to do this whole thing with Javascript before. I was able to select all values easily but after refreshing the page (which stores the selection made. This is why I need to refresh ) it didn't store it. This was no problem problem with Selenium. I guess the Select Class in Selenium does trigger a kind of click. – ma66ot Feb 14 '21 at 14:56
0

After learning more about Python and adding all I've learned to the script I finally achieved every step I mentioned in my intial posting.

The steps are:

  1. Open Chrome I'm opening webdriver.Chrome with my user data because I need to stay logged in to the CMS I'm working with. I'm achieving this by adding an argument to options

  2. Find all identical classes This particular class represents a dropdown menu

  3. Loop through all dropdowns

  4. Select value

  5. Time sleep of 1s For some reason the script sometimes won't select all values so this helped. This might have to do with the closing of the browser in the next step but I'm not sure.

  6. Close Chrome In my case this is necessary to save the selected values to the CMS

  7. Restart script I defined a while loop of 40 repetitions. Be aware of IndentationError meaning to add a tab to the looped elements (I know this is basic stuff but might be useful for a newbie like me). Otherwise the script won't work. Also see Nested for loop IndentationError: expected an indented block when using calendar module

Here is my code:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options

import time
count = 0
while count < 40:
    print(count)
    options = Options()
    options.add_argument("user-data-dir=selenium")
    driver = webdriver.Chrome(options=options)
    driver.get(
        "https://URL")

    # find all dropdown element
    list_drop_down = driver.find_elements_by_class_name("address-status")

    # select confirmed value for each dropdown
    for drop_down_element in list_drop_down:
        select = Select(drop_down_element)
        select.select_by_value('1')

    time.sleep(1)
    driver.close()
    count = count+1

Thanks to the user Trinh Phat who gave me the necessary push. My work place loves me now ;)

ma66ot
  • 47
  • 9