How to select a textbox using selenium in python - python

HTMLI want to select a textbox using XPath or any other locator, but I am unable to do so. The code is working fine for one part of the page, while it is not working using any locator for the other half of the page. I am not sure if my code is wrong or if something else is the problem.
I have attached the HTML part.
Here is my code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get('Website')
driver.implicitly_wait(50)
driver.find_element_by_xpath('//*[#id="j_username"]').send_keys("Username")
driver.find_element_by_xpath('//*[#id="j_password"]').send_keys("Password")
driver.find_element_by_xpath('//*[#id="b_submit"]').click()
driver.find_element_by_xpath('//*[#id="15301"]/div[1]/a/span').click()
driver.find_element_by_xpath('//*[#id="22261"]/a').click()
driver.find_element_by_xpath('//*[#id="22323"]/a').click()
driver.implicitly_wait(50)
driver.find_element_by_xpath('//*[#id="filterRow"]').clear()
The last line is where I am getting the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="filterRow"]"}

Page may have not finished rendering when you try to find the element. So it will give NoSuchElementException
Try the following method
elem = driver.find_element_by_xpath('//*[#id="filterRow"]')
if len(elem) > 0
elem[0].clear()
Hope this will help you

You can wait till the elements loads using wait -
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
Filter_Row = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="filterRow"]')))
Filter_Row.clear()
Try the above code and see what happens.

Try below solution
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='filterRow']"))).clear()
Note: add below imports to your solution :
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

As in one of comments, you mentioned upon clicking tab a new page is opening. Can you please check if its opening in a new frame. Is so please switch to frame first where your element is using below statement:
driver.switch_to.frame(driver.find_element_by_name(name))
To navigate back to original frame you can use:
driver.switch_to.default_content()

using 'find_element_by_css_selector'
driver.find_element_by_css_selector("input")

Related

How to Fix: Unable to locate element: method- XPath

This is the website I am trying to automate some clicks:
I have tried clicking the button using Xpath and FullXpath, but still got no luck.
This is the simple code:
w = webdriver.Chrome(executable_path='chromedriver.exe',
chrome_options=options)
w.get("https://quillbot.com/")
time.sleep(5)
pasteXpath = "//button[contains(#class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()
But it fails with this message in the console:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="inOutContainer"]/div[2]/div[2]/div/div[1]/div/div/div[1]/div/div/div[2]/div/div/button/span[1]/div"}
Please show me how to automate this click using selenium.
I recommend using By, WebDriverWait, and expected_conditions in the place of .find_element_by_xpath.
After you click the paste button you will receive a permissions prompt. See below to get past it.
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 import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.chrome.service import Service
import time
import pyautogui
service = Service('C:\\Path_To_Your\\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://quillbot.com/')
paste_button = WebDriverWait(driver, 3).until(EC.visibility_of_element_located(
(By.XPATH, "//span[text()='Paste Text']")))
paste_button.click()
time.sleep(2)
pyautogui.press('tab')
pyautogui.press('tab')
pyautogui.press('enter')
This will work:
pasteXpath = "//button[contains(#class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()
Don't forget to add some wait / delay before it to make sure the page is fully loaded.
Try to use CSS selector instead:
element = w.find_element_by_css_selector('div[class*="MuiGrid-root"] > div[class="jss473"]').click()
You can find all the doc about css selector here

Failed to locate the webdriver element using Selenium |

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()

How to create an automation for tplink pharos cpe520 using xpath with selenium and python for log in?

I'm trying to create an automation for my tplink pharos cpe520
This is the full xpath
"/html/body/div[4]/div/div[4]/div/div/div/div/div[1]/div[2]/div[1]/div[2]/div[2]/div[2]/div[1]/span[2]/input" it never change. I have to use xpath because every time the id is changed.
this is the xpath
//*[#id="widget--95952b3d-c134-3cfe-dd46-1a85b70c6882"]/div[2]/div[1]/span[2]/input
this is a new xpath
//*[#id="widget--059a411f-7134-3cfe-ec40-3c71bd80af37"]/div[2]/div[1]/span[2]/input
as you can see it changed
1
Try below xpath :
driver.find_element_by_xpath("//input[#type='text']")
or
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='text']']"))).send_keys("Test")
Note : add below impports to your solution
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
The problem was solved with "implicitly_wait(5)" before I get the link. It was not a problem from xpath.

python selenium can't interact with input element? [duplicate]

This question already has answers here:
ElementNotVisibleException: Message: element not interactable error while trying to click a button through Selenium and Python
(2 answers)
Closed 3 years ago.
I'm trying to put my name in an input field. It seems like a simple thing that selenium is built to do, but I cannot figure out what I'm doing wrong.
name = driver.find_element_by_xpath('//input[#id="signUpName16"]')
name.send_keys('Josh')
I know the driver works because I've been able to click other elements. I know the xpath is right because I copied it from chrome inspector. The error I get is
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I've seen people say to try clicking or clearing elements so I've tried that too, but that still failed.
name = driver.find_element_by_xpath('//input[#id="signUpName16"]')
name.click()
name.send_keys('Josh')
yields this for the name.click() line
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
There's a few different things that can be going wrong here. If the input is not fully loaded, then it will throw this exception if you try to send_keys before it is ready. We can invoke WebDriverWait on the input element to ensure it is fully loaded before sending keys to it:
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
input = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(#id, 'signUpName')]")))
input.send_keys("Josh")
If this still throws the exception, we can instead try to set the input value through Javascript:
input = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(#id, 'signUpName')]")))
driver.execute_script("arguments[0].value = 'Josh';", input)
If neither of these solutions work, we may need to see some of the HTML on the page you are working with to see if there's any other issue happening here.
ElementNotInteractableException occurs when
Element is not displayed,
Element is out of screen ,
Some time element is hidden or
Behind to another element
Please refer below code to solve this issue:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.set_page_load_timeout("10")
driver.get("your url")
actionChains = ActionChains(driver)
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//input[#id='signUpName16']")))
actionChains.move_to_element(element).click().perform()
Solution 2:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//input[starts-with(#id,signUpName')]"))) # if your signUpName16 element is dynamic then use contains method to locate your element
actionChains.move_to_element(element).click().perform()

Element Exception

I am trying to execute this code and it shows 'NoSuchElementException' this error.
So can anyone help me for this ?
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver= webdriver.Chrome()
page=driver.get("http://www.awgp.org")
banner_tell=driver.find_element_by_Link_text('Tell Me More')
banner_tell.click()
I think all you need to do is give link text in uppercase but this link is dynamic as the banner auto-slides to the next one.
You should come up with another locator to click on exactly the locator you want to click. Otherwise you might get ElementNotVisibleException if the banner is changed.
banner_tell=driver.find_element_by_link_text('TELL ME MORE')
try with xpath
banner_tell= driver.find_element_by_xpath("//*[contains(text(),'TELL ME MORE')]")
Seems you were almost near however the function() should have been :
find_element_by_link_text()
To click on the button with text as TELL ME MORE you have to induce WebDriverWait with expected_conditions clause element_to_be_clickable as follows :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "TELL ME MORE"))).click()

Categories

Resources