0

I'm trying to make my bot to click on a drop down menu, select option A and refresh the page. The problem I'm having is that I can't use Select as I get error message saying that it can't use select on . Here is the inspection of the drop down in question. How do I make it to select option A?

<div class="Select-value"><span class="Select-value-label" role="option" aria-selected="true" 
id="react-select-2--value-item">CrazyFortune</span></div>

I can't share the website as it contains sensitive info but here is my code:

import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
import openpyxl
import xlsxwriter

#my private module with sensitive info
import pwx_module as pwx

#delay libraries
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

#imports
driver_path = r'C:\Users\name\chromedriver.exe'
outcome = openpyxl.load_workbook(r'C:\Users\name\outcome.xlsx')
driver = webdriver.Chrome(driver_path)   
driver.get('link_hidden')

# waiting for login table to load
try:
    element = WebDriverWait(driver,10).until(
    ec.presence_of_element_located((By.XPATH,'//*[@id="email"]'))
    )
except:
    driver.quit()

#entering sensitive info
driver.find_element_by_id("email").send_keys(pwx.em)                        
driver.find_element_by_id("password").send_keys(pwx.pw)                               
driver.find_element_by_xpath('//* 
[@id="appContainer"]/div/form/button').click()      

# waiting for page to load
try:
    element = WebDriverWait(driver,10).until(
    ec.presence_of_element_located((By.XPATH,'//* [@id="testing"]/section/section[1]/div[1]/header/div/div/div[1]/div[2]/div''))
    )
except:
    driver.quit()


# THIS IS WHERE THE PROBLEM IS!!! - code above works fine
drp = select(driver.find_element_by_xpath('//* 
[@id="testing"]/section/section[1]/div[1]/header/div/div/div[1]/div[2]/div'))
drp.select_by_visible_info('A')
driver.refresh()

# writing info into excel file, code below works fine
cell = outcome['import']    
withdrawal_cell = 'B19'     
cell[withdrawal_cell].value = w.text

driver.quit()

outcome.save(r'C:\Users\giuse\Desktop\Back office bot\outcome.xlsx')
2
  • Could you include the code you have written and the link to the page with the dropdown. Commented Jun 17, 2020 at 10:59
  • I can include the code, but unfortunately I'm not allowed to share the website (contains sensitive info) Commented Jun 17, 2020 at 11:36

1 Answer 1

1

Try to pass the correct xpath

driver.find_element_by_xpath("//div/span[@id='react-select-2--value-item']").click()
Sign up to request clarification or add additional context in comments.

7 Comments

Actually, this code clicks on a dropdown successfully but then can't change the dropdown options :(
try to perform another click on the option that you want to select, please provide DOM for options
The problem is they both seem to have the same xpath! option A looks like this: <span class="Select-value-label" role="option" aria-selected="true" id="react-select-2--value-item">A</span> Option B looks like this: <span class="Select-value-label" role="option" aria-selected="true" id="react-select-2--value-item">B</span> But can I point out into the inside html?
I noticed a change inside of the html: option A has value "27", option B has value "32". Can I somehow interact with the page to change the value? <input type="hidden" name="portals" value="32">
yes, you need to provide correct xpath and click option: driver.find_element_by_xpath("//span[@role='option' and text()='A']").click() or you could use Select class : selectElement = Select(find_element_by_xpath("//div/span[@id='react-select-2--value-item']")) selectElement.select_by_visible_text("A") or by value selectElement.select_by_value('27')
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.