Selenium not clicking search box - python

I am trying to have selenium to do the following:
Open a website
Click on the search box
Type "Seattle" in the search box
Select the first result from the suggested results
Hit Enter
Click on the new search box
Type "Chicago" in the new search box
Select the first result from the suggested results
I was able to get it to work until step 5, but I can't find a way to do the same tasks with a new search box in step 6.
Here's my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Seattle')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.ng_pristine"))).send_keys(Keys.ENTER)
Here's what the source page of the new search box looks like:
This is the final result I want to see:

In this specific scenario, to make a new search you should first clear the previous search state.
This should work:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Seattle')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
# clear the previous search results
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".btn_close"))).click()
#perform a new search
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys('Chicago')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search"))).send_keys(Keys.ENTER)
UPD
For the edited question the answer is:
You are using a wrong locator.
This should work:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
url = 'https://wego.here.com/'
driver.get(url)
search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input.input_search")))
search_input.click()
search_input.send_keys('Seattle')
search_input.send_keys(Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn"))).send_keys(Keys.ENTER)
internal_search_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input#itinerary_item_input_0")))
internal_search_input.click()
internal_search_input.send_keys('Chicago')
internal_search_input.send_keys(Keys.ENTER)

Related

Why is Selenium not finding an element

I wrote this short programm to login in to my postfield:
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.web.de/')
time.sleep(2)
frame = driver.find_element_by_name('landingpage')
# do something with frame ...
driver.switch_to.frame(frame)
innerFrame = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(innerFrame)
driver.find_element_by_id("save-all-conditionally").click()
time.sleep(2)
driver.switch_to.default_content()
time.sleep(3)
inputemail = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/div[2]/div/input')
inputpwd = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/div[3]/div/input')
buttonsend = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/button')
inputemail.send_keys('xxx#web.de')
inputpwd.send_keys('xxx')
buttonsend.click()
time.sleep(3)
frame2 = driver.find_element_by_name('home')
driver.switch_to.frame(frame2)
linkwrite = driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[2]/div[1]/div[6]/div[1]/ul/li/section/div[3]/div/div[1]/a')
linkwrite.click()
This part is working fine with the iframes there. My next goal was then to fill out an input field. The Code of the page which opened after the sign in progress is the one on the picture: https://www.transfernow.net/dl/20210919Porfd5N7
But the Code for filling:
frame3 = driver.find_element_by_xpath('...')
driver.switch_to.frame(frame3)
fillin = driver.find_element_by_xpath('/html/body/div[3]/div[3]/div[3]/div[1]/div[1]/div/form/div[2]/div[1]/div[2]/div[1]/div[1]/div[2]/div/div/ul/li/input')
fillin.send_keys('hello')
has resulted in:
"Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/nav/div[2]/div[1]/div[2]/a[1]"}"
Where's my mistake?
Please help me!
Few things
There are nested iframe, first one is iframe[name='landingpage'] (located by css selector) second one is iframe[sandbox*='allow-popups'][style^='display'] and then you can click on Zustimmen und weiter button.
The xpath that you are using is absolute, try using relative xpath, if possible switch to css_selector.
Use Explicit waits.
Code :-
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 20)
driver.get("https://web.de/consent-management/")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name='landingpage']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[sandbox*='allow-popups'][style^='display']")))
wait.until(EC.element_to_be_clickable((By.ID, "save-all-conditionally"))).click()
driver.switch_to.default_content()
inputemail = wait.until(EC.element_to_be_clickable((By.ID, "freemailLoginUsername")))
inputpwd = wait.until(EC.element_to_be_clickable((By.ID, "freemailLoginPassword")))
buttonsend = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Login']")))
inputemail.send_keys('xxx#web.de')
inputpwd.send_keys('xxx')
buttonsend.click()
time.sleep(3)
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Additionally you can use the below code :
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pos-input input"))).send_keys('WEB.DE E-Mail-Adresse')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pos-input input[name^='password']"))).send_keys('password here')
to fill details on Bitte erneut einloggen page.

selenium more result xpath

im trying to print all links but i have an error ( ut is not clickable at point (781,748) because another element obscures it
the code updated:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
#driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver = webdriver.Firefox(executable_path='geckodriver')
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get("https://www.architectes-pour-tous.fr/")
driver.find_element_by_xpath("//button[contains(#class,'decline-button')]").click();
driver.find_element_by_xpath(".//a[#id='pager']").click();
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.image-projet img")))
time.sleep(1)
for img in driver.find_elements_by_css_selector('div.image-projet img'):
print(a.get_attribute('href'))
driver.find_elements_by_css_selector('button.agree-button').click()
pager = driver.find_element_by_xpath('//*[#id="pager"]')
actions.move_to_element(pager).build().perform()
time.sleep(0.5)
pager.click()
You have to accept/decline the cookies before accessing any element on the page.
driver.find_element_by_xpath("//button[contains(#class,'decline-button')]").click();
driver.find_element_by_xpath(".//a[#id='pager']").click();
The element you trying to access is initially out of the visible screen so you have to scroll to it before clicking it.
Also possibly you will have to close the accept cookies pop-up prior to clicking this element.
Also I'm quite sure you are getting no links with
for a in driver.find_elements_by_xpath('.//a'):
print(a.get_attribute('href'))
since you trying to do that before the page is loaded.
Also, if you are trying to get the search results links you have to use another locator.
So would suggest to change your code as following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
#driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver = webdriver.Firefox(executable_path='geckodriver')
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get("https://www.architectes-pour-tous.fr/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.image-projet img")))
time.sleep(1)
for img in driver.find_elements_by_css_selector('div.image-projet img'):
print(a.get_attribute('href'))
driver.find_element_by_css_selector('button.agree-button').click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
pager = driver.find_element_by_xpath('//*[#id="pager"]')
actions.move_to_element(pager).perform()
time.sleep(0.5)
pager.click()

Why is this selenium code not working always?

Why does this works only sometimes? Sometimes it navigates easily and sometimes it gets stuck after selecting a category. I want to click the category button and select notebook then the series button and click "acer one" and then the first model from model menu. But somehow it gets stuck sometimes after category and sometimes after series.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
import time
import urllib.request
PATH = r"D:\Py\Selenium\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://www.acer.com/ac/en/GB/content/support')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight/5);")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[1]/a"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[1]/ul/li[1]/a"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[2]/a"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[2]/ul/li[2]"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[3]/div/div[3]/div/div[3]/a"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[7]/div[3]/div/div[3]/div/div[3]/ul/li[1]"))).click()
src = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"/html/body/div[7]/div[2]/div/div/div[1]/img"))).get_attribute("src")
urllib.request.urlretrieve(str(src),"img.png")
time.sleep(2)
driver.quit()
# search.send_keys("m")
I'm really not sure why your code is failing, but i have tried this code which i ran multiple times and it is not failing. Please give it a try and do let me know if you have any issues running this script. Please find the code below -
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import urllib.request
driver = webdriver.Chrome()
driver.get('https://www.acer.com/ac/en/GB/content/support')
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Category')]"))).click()
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//a[contains(text(),'Category')]/following-sibling::ul/li/a[text()='Notebook']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Series')]"))).click()
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//a[contains(text(),'Series')]/following-sibling::ul/li/a[text()='Acer One']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Model')]"))).click()
wait.until(EC.element_to_be_clickable(
(By.XPATH, "//a[contains(text(),'Model')]/following-sibling::ul/li[1]"))).click()
src = wait.until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[2]/div/div/div[1]/img"))).get_attribute("src")
urllib.request.urlretrieve(str(src), "img.png")
time.sleep(2)
driver.quit()
I have slightly modified your code. Thank You..!

Clicking on each div from a container in Selenium

I am using selenium to automate the access to https://www.nemlig.com/ 's pages and I don't know how to iterate through (let's say) 8 div, all contained in another div.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = r'C:\Users\user\lib\chromedriver_77.0.3865.40.exe')
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("https://www.nemlig.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".timeslot-prompt.initial-animation-done")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='tel'][class^='pro']"))).send_keys('2300')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.prompt__button"))).click()
Above is the code so far. After completing this, I want to access the date buttons one by one at a given time interval.
I have issues achieving this as all of them look the same in HTML. How can I tell the webdriver to click the next div inside the container until the condition that all of the 8 dates have been accessed?
Induce WebDriverWait and presence_of_all_elements_located() and following CSS selector. I have added a date checks to check that if date in not available in the list then click on that date.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome(executable_path = r'C:\Users\user\lib\chromedriver_77.0.3865.40.exe')
wait = WebDriverWait(driver,20)
driver.maximize_window()
driver.get("https://www.nemlig.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".timeslot-prompt.initial-animation-done")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='tel'][class^='pro']"))).send_keys('2300')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.prompt__button"))).click()
dates=[]
elements=wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"div[data-automation='dayDateTmSlt']")))
for ele in elements:
if ele.text not in dates:
dates.append(ele.text)
driver.execute_script("arguments[0].click();", ele)
time.sleep(3)

Python Selenium Xpath from firebug not found

I am trying to login to the ESPN website using selenium. Here is my code thus far
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.maximize_window()
url = "http://www.espn.com/fantasy/"
driver.get(url)
login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
except:
driver.quit()
Basically, there are 2 steps, first I have to click the login button and then I have to fill in the form. Currently, I am clicking the login button and the form is popping up but then I can't find the form. I have been using firebug to get the xpath as suggested in other SO questions. I don't really know much about selenium so I am not sure where to look
Try to use
driver.switch_to_frame('disneyid-iframe')
# handle authorization pop-up
driver.switch_to_default_content() # if required
This works for me, switching to the iframe first. Note that you will need to switch back out of the iframe after entering the credentials.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.maximize_window()
url = "http://www.espn.com/fantasy/"
driver.get(url)
login_button = driver.find_element_by_xpath("/html/body/div[6]/section/section/div/section[1]/div/div[1]/div[2]/a[2]")
login_button.click()
iframe = driver.find_element_by_id("disneyid-iframe")
driver.switch_to.frame(iframe)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "/html/body/div[2]/div/div/section/section/form/section/div[1]/div/label/span[2]/input")))
element.send_keys("my username")
import time
time.sleep(100)
finally:
driver.quit()

Categories

Resources