Using selenium automation webdriver i'm unable to locate the text box element on a travel website using python. Using locator present in the webdriver such as Id/name/css_selector/class_name or xpath/full xpath.
Below is the screenshot of the python code:
[Code_1][1]
While the first one is located the second one isn't. The corresponding HTML code is
[text_box2][2]
How can i fill(automate) both fileds corresponding flight destinations i.e leaving and going
[1]: https://i.stack.imgur.com/gpoIr.jpg
[2]: https://i.stack.imgur.com/Q7mGs.jpg
Here is a slightly different approach for locating things. I am using waits borrowed from CruisePandey in this thread. I used firefox, but that is adaptable. Some notes of what was hard:
I had to make sure to be on the Flights tab.
I had to click in the from and to fields, which were buttons after all, and then wait to be able to type into the revealed input fields.
Finally, I had to choose the first element from the dropdown list that resulted.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver')
driver.maximize_window()
driver.get('https://www.expedia.co.in')
wait = WebDriverWait(driver, 15)
wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Flights")))
driver.find_element_by_link_text('Flights').click()
wait.until(EC.presence_of_element_located((By.ID, "location-field-leg1-origin")))
driver.find_element(By.CLASS_NAME,'uitk-faux-input').click()
fieldInput = driver.find_element_by_id('location-field-leg1-origin')
wait.until(EC.visibility_of(fieldInput))
fieldInput.send_keys("SFO")
wait.until(EC.presence_of_element_located((By.TAG_NAME, "strong")))
driver.find_element_by_tag_name('strong').click()
driver.find_elements(By.CLASS_NAME,'uitk-faux-input')[1].click()
fieldInput = driver.find_element_by_id('location-field-leg1-destination')
wait.until(EC.visibility_of(fieldInput))
fieldInput.send_keys("BOS")
wait.until(EC.presence_of_element_located((By.XPATH,"//strong[contains(text(), 'Boston')]")))
driver.find_element_by_xpath("//strong[contains(text(), 'Boston')]").click()
You may want to use the below xpath for going To input field :
//label[text()='Going to']//following-sibling::input[#name]
Code :
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[text()='Going to']//following-sibling::input[#name]"))).send_keys('something')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
if you want to automate from 1 place holder to another, i have use this set of code & it works for me
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[#id='location-field-leg1-origin-menu']/div[2]/ul/li[1]/button"))).click()
b = wait.until(EC.element_to_be_clickable((By.XPATH, ".//*[#id='location-field-leg1-destination-menu']/div[1]/button"))).send_keys("NYC")
b = wait.until(EC.element_to_be_clickable((By.XPATH,".//*[#id='location-field-leg1-destination-menu']/div[2]/ul/li[1]/button"))).click()
c = wait.until(EC.element_to_be_clickable((By.XPATH, ".//*[#id='d1-btn']"))).click()
Related
Trying to automate one thing for my work, which was choosing one option from the dropdown list on the website below:
https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ
The Selenium automation didn't work in that case. After writing such code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_driver_path = r"C:/Users/.../Projects/chromedriver.exe"
service = Service(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service)
driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
wait = WebDriverWait(driver, 10)
abo_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="combobox-button-53"]')))
After executing I've got a message:
TimeoutException
In case of finding element by tag name or any other options, the following message pops up:
Message: no such element: Unable to locate element
The list is built on button tags and has a structure of lightning-basecombobox. It looks like there is no possibility to click on the dropdown list and choose the required option automatically.
Is it needed to do something different with such stuff?
What I expect is to use Selenium to choose between the options in the list.
The combobox is a <button> element.
Additionally the id i.e. combobox-button-53 is dynamically generated and is bound to chage sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.
Solution
To click element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[name='subscriptionsTypeId']"))).click()
Using XPATH:
driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#name='subscriptionsTypeId']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot;
When I load the page, I get the element ID (XPATH) as following. //*[#id="combobox-button-51"]
Maybe the number 51 isn't always the same? In that case, try:
//*[starts-with(#id,'combobox-button-5')]
Or just use //*[#name="subscriptionsTypeId"] , as another answer here allready mentioned.
I'm trying to click this button shown after choosing an option from a drop-down menu.
This is the button I'm trying to click.
The link to the website: https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx
The html:
I've tried using XPATH, and through visible text; nothing seems to work.
My code as of now:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx")
drop_dist = \
driver.find_element(By.XPATH, "/html/body/form/div[3]/main/div[2]/div/div[2]/div/div[1]/div[2]/div/select")
select_dist = Select(drop_dist)
select_dist.select_by_value("005")
l = driver.find_element(By.XPATH, "/html/body/form/div[3]/main/div[2]/div/div[2]/div/div[4]/div/div/table/tbody/tr[1]/td/div/div[2]/div[2]/div[1]/a").click()
time.sleep(30)
Any help is much appreciated!
The locator seems fragile, use following locator and webdriverwait() to handle sync issue.
driver.get("https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx")
wait=WebDriverWait(driver, 20)
select_dist =Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#ctl00_ContentPlaceHolder1_ddl_District"))))
select_dist.select_by_value("005")
wait.until(EC.element_to_be_clickable((By.XPATH, "(//a[#class='btn btn-link' and contains(., 'View Detail Break up')])[1]"))).click() //this will click the first one only.
You have to add following imports.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try this:
button = driver.find_elements(By.CSS, 'a[role="button"][aria-expanded="false"]')[0]
button.click()
You can change the index in order to click on other elements
I currently wish to select a value from a drop-down menu in python with selenium.
The site is the following: https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M
My current code
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.Safari(executable_path = '/usr/bin/safaridriver')
wait = WebDriverWait(driver, 20)
driver.get("https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='form-group' and .//label[#for='programok']]//div[contains(#class,'css-1wy0on6')]"))).click()
time.sleep(1)
current_program = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class,'css-26l3qy-menu')]")))
current_program.click()
My issue is that I am not able to find out the needed xpath code for the pop-upped drop-down menu, since I can see it in developer view only if the list is clicked, but I can see then only that "//div[contains(#class,'css-26l3qy-menu')]" is a suitable xpath for the menu. (see the enclosed picture)
But it chooses a random value. How specify which value I want to select?
the link I'm trying to scrape is https://hcad.org/property-search/real-property/real-property-search-by-account-number/
I'm using this method currently
driver.get('https://hcad.org/property-search/real-property/real-property-search-by-account-number/')
driver.implicitly_wait(2)
driver.find_element_by_xpath('/html/body/div[1]/div[3]/div/h1').click()
actions.send_keys(Keys.TAB * 2 )
actions.send_keys(account_num)
actions.send_keys(Keys.TAB)
actions.send_keys(Keys.RETURN)
actions.perform()
to type the account number in search box and search it which gives me a new form.
I tried to find the input area using full xpath but it never works.
Is there is anyway I can write account num using element xpath or I can excess the form elements other then actions method.
I tried different methods.
like searching input area using (id,full xpath) using driver implicity wait function but none of them work please help me here so that I can grab elements using your method on the form I get after typing account number I'm lost.
You are using wrong locator.
Also, I don't understand why are you using Actions instead of Selenium.send_keys()?
I would do this as following:
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.keys import Keys
wait = WebDriverWait(driver, 20)
driver.get('https://hcad.org/property-search/real-property/real-property-search-by-account-number/')
wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath("//iframe")))
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[name="searchval"]'))).send_keys(account_num)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[type="submit"]'))).click()
The search field is in iframe, you would need to switch the driver focus :
code :
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get('https://hcad.org/property-search/real-property/real-property-search-by-account-number/')
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src='https://public.hcad.org/records/Real.asp']")))
driver.find_element_by_id('acct').send_keys("12345")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[value='Search']"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I'm trying to input an id into this website, however, anything I search for inside of the table shows up as "unable to locate element".
id_field = driver.find_element_by_xpath('//*[#id="p01aForm_b_studentId"]')
doesn't seem to provide the element.
driver = webdriver.Chrome()
driver.get("https://www.e-license.jp/el25/?abc=lEPHbvBGJVY%2BbrGQYS%2B1OA%3D%3D")
login_link = driver.find_element_by_xpath('//a[#href="'+"https://www.e-license.jp/el25/?abc=lEPHbvBGJVY%2BbrGQYS%2B1OA%3D%3D"+'"]')
login_link.click()
driver.find_element_by_id("p01aForm_b_studentId")
If you want to click that link try this. I'm not sure why your trying to open up the tab if you want the first site's id.
wait = WebDriverWait(driver, 10)
driver.get("https://www.e-license.jp/el25/?abc=lEPHbvBGJVY%2BbrGQYS%2B1OA%3D%3D")
wait.until(EC.element_to_be_clickable((By.ID, "p01aForm_b_studentId"))).send_keys("a")
#wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href,'https://www.e-license')]"))).click()
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC