johnnnieyy
johnnnieyy

Reputation: 11

Selecting an item from a dropdown menu using Python/Selenium

I am trying to fill out a form for states where I would select one at the bottom Oregon. From https://www.adidas.com/us/delivery-start. New to coding thank you!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select


state = driver.find_element_by_xpath('//*[@id="dwfrm_delivery"]/div[2]/div[2]/div/fieldset/div/div[1]/div[6]/div[1]/div/div/a/span')
stateselect = state.select_by_visible_test("Oregon")

This is the error I am getting

AttributeError: 'WebElement' object has no attribute 'select_by_visible_test'.

And with

state = driver.find_element_by_xpath('//*[@id="dwfrm_delivery"]/div[2]/div[2]/div/fieldset/div/div[1]/div[6]/div[1]/div/div/a/span')
stateselect = state.send_keys("Oregon")

I get the following :

error:WebDriverException: Message: unknown error: cannot focus element

Please help me understand the error for both cases to further my understanding. And point me in the right direction.

Upvotes: 0

Views: 237

Answers (1)

Mark Lapierre
Mark Lapierre

Reputation: 1087

When you're copying code always double-check you've copied it correctly.

select_by_visible_test should be select_by_visible_text

But that won't work because you're not using the select class. Before using stateselect you need:

stateselect = Select(state)

And then you can do:

stateselect.select_by_visible_text("Oregon")

You should also check the official documentation. In this case, the documentation for the python selenium api for WebElement and Select. You can almost always find the documentation by searching for, e.g., "python selenium select"

Upvotes: 1

Related Questions