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')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ellie Biessek
  • 148
  • 2
  • 14

1 Answers1

1

Try to pass the correct xpath

driver.find_element_by_xpath("//div/span[@id='react-select-2--value-item']").click()
  • Actually, this code clicks on a dropdown successfully but then can't change the dropdown options :( – Ellie Biessek Jun 17 '20 at 12:12
  • try to perform another click on the option that you want to select, please provide DOM for options – adambogdan1993 Jun 17 '20 at 12:38
  • The problem is they both seem to have the same xpath! option A looks like this: A Option B looks like this: B But can I point out into the inside html? – Ellie Biessek Jun 17 '20 at 13:03
  • 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? – Ellie Biessek Jun 17 '20 at 13:08
  • 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') – adambogdan1993 Jun 17 '20 at 13:16
  • they both have exactly same xpath, so my problem is that I dpn't seem to be able to change the option – Ellie Biessek Jun 17 '20 at 13:18
  • I edited comment, please try and tell me if it worked – adambogdan1993 Jun 17 '20 at 13:21