'How to interact with a dropdown menu in python selenium

The project I am working on requires me to interact with a dropdown age selection menu on the Instagram signup website.

What the whole script is intended to do is to create Instagram accounts with third party website emails and usernames, These then will do things such as like posts follow accounts etc. I have the first page of sign up complete where you have to put a username email password etc. The code below is what a have wrote to try and select a month from the birthday dropdown menu.

bmonth = driver1.find_element(By.XPATH, "//*[@id="react-root"]/section/main/div/div/div[1]/div/div[4]/div/div/span/span[1]/select/option[5]")
bmonth.click()

The XPATH in this line of code is to one of the options in the dropdown menu directly.

This code obviously doesn't work and I have no idea how I can do this. I cant copy the HTML code but it is the birthday section of Instagram's signup page. The menus do not have ID's. (Please bear with me on formatting of all the following code and question, this is my first post and I am a fairly beginner coder).

In my XPATH that I copied, the id "react-root" is unresolved, both words React and Root show up in the problems section of pycharm as being unresolved.

I left my imports out of the below code because they were formatting horribly. (Below is my whole script)

Everything works good up until the dropdown menus, my code is probably far from correct so some help would be appreciated! Even open to suggestions on how to make my already working code run more efficiently or clean it up!

while True:

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver1 = webdriver.Chrome(PATH)

driver1.get("https://www.instagram.com/accounts/emailsignup/?hl=en")

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver2 = webdriver.Chrome(PATH)

driver2.get("https://10minutemail.net/")

# copy email from generator
try:
    emailgen = WebDriverWait(driver2, 10).until(
        EC.presence_of_element_located((By.ID, "copy-button"))

    )
finally:
    emailgen = driver2.find_element(By.ID, "copy-button")
    emailgen.click()
    driver2.minimize_window()

# paste email into instagram
try:
    email = WebDriverWait(driver1, 10).until(
        EC.presence_of_element_located((By.NAME, "emailOrPhone"))

    )
finally:
    email = driver1.find_element(By.NAME, "emailOrPhone")
    email.click()
    email.send_keys(Keys.CONTROL + "v")

# paste name into instagram
try:
    name = WebDriverWait(driver1, 10).until(
        EC.presence_of_element_located((By.NAME, "fullName"))

    )
finally:
    name = driver1.find_element(By.NAME, "fullName")
    name.click()
    name.send_keys("Frank Test")

# open username generator
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver3 = webdriver.Chrome(PATH)

driver3.get("https://1password.com/username-generator/")

try:
    generate = WebDriverWait(driver3, 10).until(
        EC.presence_of_element_located((By.ID, "copy-secure-password"))

    )
finally:
    generate = driver3.find_element(By.ID, "copy-secure-password")
    time.sleep(3)
    generate.click()
    driver3.minimize_window()

# paste username into instagram

try:
    user = WebDriverWait(driver1, 10).until(
        EC.presence_of_element_located((By.NAME, "username"))

    )
finally:
    user = driver1.find_element(By.NAME, "username")
    user.click()
    user.send_keys(Keys.CONTROL + "v")

# enter password

try:
    password = WebDriverWait(driver1, 10).until(
        EC.presence_of_element_located((By.NAME, "password"))

    )
finally:
    password = driver1.find_element(By.NAME, "password")
    password.click()
    password.send_keys(":j}yAsUp673ds.JA")

time.sleep(3)

actions = ActionChains(driver1)
actions.send_keys(Keys.ENTER)
actions.perform()

bmonth = driver1.find_element(By.XPATH, "//*[@id="react-root"]/section/main/div/div/div[1]/div/div[4]/div/div/span/span[1]/select/option[5]")
bmonth.click()

This is the error code pycharm throws at me. (Sorry for possibly bad formatting again)

Traceback (most recent call last):
  File "C:\Users\linde\insta\main.py", line 19, in <module>
    driver2.get("https://10minutemail.net/")
  File "C:\Users\linde\AppData\Local\Programs\Python\Python310\lib\site- 
packages\selenium\webdriver\remote\webdriver.py", line 436, in get
     self.execute(Command.GET, {'url': url})
   File "C:\Users\linde\AppData\Local\Programs\Python\Python310\lib\site- 
 packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
     self.error_handler.check_response(response)
   File "C:\Users\linde\AppData\Local\Programs\Python\Python310\lib\site- 
packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
     raise exception_class(message, screen, stacktrace)
 selenium.common.exceptions.WebDriverException: Message: unknown error: cannot determine 
 loading status
 from disconnected: received Inspector.detached event
   (Session info: chrome=99.0.4844.51)
 Stacktrace:
 Backtrace:
     Ordinal0 [0x002D69A3+2582947]
     Ordinal0 [0x0026A6D1+2139857]
      Ordinal0 [0x00163A98+1063576]
     Ordinal0 [0x00156410+1008656]
     Ordinal0 [0x00155FE3+1007587]
      Ordinal0 [0x001555F0+1005040]
     Ordinal0 [0x00154727+1001255]
     Ordinal0 [0x00154AF7+1002231]
     Ordinal0 [0x00160251+1049169]
     Ordinal0 [0x00155D9D+1007005]
     Ordinal0 [0x00156A0E+1010190]
     Ordinal0 [0x00155FFE+1007614]
     Ordinal0 [0x001555F0+1005040]
     Ordinal0 [0x00154727+1001255]
     Ordinal0 [0x001549E6+1001958]
     Ordinal0 [0x0016518A+1069450]
     Ordinal0 [0x001B88FD+1411325]
     Ordinal0 [0x001A854C+1344844]
     Ordinal0 [0x001B834A+1409866]
     Ordinal0 [0x001A8366+1344358]
     Ordinal0 [0x00185176+1200502]
     Ordinal0 [0x00186066+1204326]
     GetHandleVerifier [0x0047BE02+1675858]
     GetHandleVerifier [0x0053036C+2414524]
     GetHandleVerifier [0x0036BB01+560977]
     GetHandleVerifier [0x0036A8D3+556323]
     Ordinal0 [0x0027020E+2163214]
     Ordinal0 [0x00275078+2183288]
     Ordinal0 [0x002751C0+2183616]
     Ordinal0 [0x0027EE1C+2223644]
     BaseThreadInitThunk [0x75FCFA29+25]
     RtlGetAppContainerNamedObjectPath [0x77287A9E+286]
     RtlGetAppContainerNamedObjectPath [0x77287A6E+238]
     (No symbol) [0x00000000]

Process finished with exit code 1



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source