Problem with combo box using selenium doesn't change the second option
Question:
I am doing web scraping to this page https://www.tudrogueriavirtual.com/ and when Selenium do the first charge to the page, I need to select the state and the city, I tried using .click
select_state = driver.find_element('xpath', '//*[@id="ship-state"]').click()
time.sleep(1)
select_state_c = driver.find_element('xpath', '//select[@id="ship-state"]/option[@value="Bogotá, D.C."]').click()
time.sleep(1)
select_state = driver.find_element('xpath', '//*[@id="ship-state"]').click()
And this not working. I also tried using .select but this didnt work
select_state = Select(driver.find_element('xpath', '//*[@id="ship-state"]'))
select_state.select_by_value("Bogotá, D.C.")
time.sleep(1)
This occurred when I execute my code.
enter image description here
And this is what the page suposed show me
this is my Selenium version selenium==4.4.3
Answers:
The easiest way to do this is to use send_keys
like this.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.tudrogueriavirtual.com/")
time.sleep(1)
state = driver.find_element(By.ID, 'ship-state')
state.send_keys('Bogotá')
state.click()
time.sleep(1)
city = driver.find_element(By.ID, 'ship-city')
city.send_keys('Bogotá, D.c.')
city.click()
time.sleep(1)
driver.find_element(By.CLASS_NAME, 'nextBtn.btn-ubicacion').click()
time.sleep(1)
driver.find_elements(By.CLASS_NAME, 'btnFinalizar')[1].click()
However rather than use time.sleep()
I would recommend using the built in waits see https://selenium-python.readthedocs.io/waits.html
I am doing web scraping to this page https://www.tudrogueriavirtual.com/ and when Selenium do the first charge to the page, I need to select the state and the city, I tried using .click
select_state = driver.find_element('xpath', '//*[@id="ship-state"]').click()
time.sleep(1)
select_state_c = driver.find_element('xpath', '//select[@id="ship-state"]/option[@value="Bogotá, D.C."]').click()
time.sleep(1)
select_state = driver.find_element('xpath', '//*[@id="ship-state"]').click()
And this not working. I also tried using .select but this didnt work
select_state = Select(driver.find_element('xpath', '//*[@id="ship-state"]'))
select_state.select_by_value("Bogotá, D.C.")
time.sleep(1)
This occurred when I execute my code.
enter image description here
And this is what the page suposed show me
this is my Selenium version selenium==4.4.3
The easiest way to do this is to use send_keys
like this.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.tudrogueriavirtual.com/")
time.sleep(1)
state = driver.find_element(By.ID, 'ship-state')
state.send_keys('Bogotá')
state.click()
time.sleep(1)
city = driver.find_element(By.ID, 'ship-city')
city.send_keys('Bogotá, D.c.')
city.click()
time.sleep(1)
driver.find_element(By.CLASS_NAME, 'nextBtn.btn-ubicacion').click()
time.sleep(1)
driver.find_elements(By.CLASS_NAME, 'btnFinalizar')[1].click()
However rather than use time.sleep()
I would recommend using the built in waits see https://selenium-python.readthedocs.io/waits.html