I am trying to build my first python selenium bot that checks me into southwest automatically. I've got the code to open southwests' check-in site, but it doesn't type anything. Can anyone tell me what I am doing wrong?
from selenium import webdriver
driver = webdriver.Chrome('/Users/thomas****/chromedriver/chromedriver')
driver.get('https://www.southwest.com/air/check-in/index.html')
conf_num = driver.find_element_by_id("confirmationNumber")
first_name = driver.find_element_by_id("passengerFirstName")
last_name = driver.find_element_by_id("passengerLastName")
conf_num.send_keys("1234")
first_name.send_keys("Thomas")
last_name.send_keys("****")
submit = driver.find_element_by_id('form-mixin--submit-button')
submit.click()
CodePudding user response:
I just checked, it is working. Your only issue is that '1234'
is not a valid confirmation number. Look closely and it shows Enter valid confirmation #.
under the Confirmation # field.
Replace it with something like the following, for example, and you will see a bigger error.
conf_num.send_keys("977089")
CodePudding user response:
I use don't use the function find_element_by_id()
.
Try using By.
conf_num = driver.find_element(By.ID, "confirmationNumber")
first_name = driver.find_element(By.ID, "passengerFirstName")
last_name = driver.find_element(By.ID, "passengerLastName")
...
submit = driver.find_element(By.ID, 'form-mixin--submit-button')
Note that you have to import:
from selenium.webdriver.common.by import By
CodePudding user response:
Looks like you are missing a delay. You need to wait for elements to be loaded and be ready to accept inputs. WebDriverWait
is used for that. Also, all the find_element_by_*
are obsolete now. New syntax should be used instead as following:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('/Users/thomas****/chromedriver/chromedriver')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://www.southwest.com/air/check-in/index.html"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.ID, "confirmationNumber"))).send_keys("1234")
wait.until(EC.element_to_be_clickable((By.ID, "passengerFirstName"))).send_keys("Thomas")
wait.until(EC.element_to_be_clickable((By.ID, "passengerLastName"))).send_keys("****")
wait.until(EC.element_to_be_clickable((By.ID, "form-mixin--submit-button"))).click()