How to find_element(By.XPATH) and sending keys in selenium? - python

Trying to update my code to use "driver.find_element(By.XPATH..." instead of "driver.find_elements_by_xpath(...", but I keep getting the following the error when I send keys:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Here is my code:
driver = webdriver.Chrome(PATH)
link_login = "https://www.wyzant.com/tutor/jobs"
driver.get(link_login)
username_input = driver.find_element(By.XPATH, "//*[#id='Username']")[1]
username_input.send_keys("Test")

Use find_elements instead of find_element to select the element like you do in your example:
driver.get('https://www.wyzant.com/tutor/jobs')
username_input = driver.find_elements(By.XPATH, "//*[#id='Username']")[1]
username_input.send_keys("Test")
or change your xpath to select more specific '//form[#class="sso-login-form"]//*[#id="Username"]':
driver.get('https://www.wyzant.com/tutor/jobs')
username_input = driver.find_element(By.XPATH, '//form[#class="sso-login-form"]//*[#id="Username"]')
username_input.send_keys("Test")

Try using more precise XPath locator.
The entire XPath expression should be inside the (By.XPATH, "your_xpath_expression")
You should also use expected conditions explicit waits
This should work:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[#action='/sso/login']//input[#id='Username']"))).send_keys(your_user_name)
You will need to import these imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

To send a character sequence within the Username field 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://www.wyzant.com/tutor/jobs")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form.sso-login-form input#Username"))).send_keys("rushi")
Using XPATH:
driver.get("https://www.wyzant.com/tutor/jobs")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[#class='sso-login-form']//input[#id='Username']"))).send_keys("rushi")
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

Related

How to use driver.find_element() to locate an element and click on it using Selenium

I want to ask aobut driver.find_element(). I want to make the automatic site login with a chrome driver and python. I want to click the login button, but it doesn't work.
Here is code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
driver.get('https://www.naver.com')
time.sleep(2)
search = driver.find_element(By.CLASS_NAME,'link_login')
search.click()
I also used
search = driver.find_element(By.CLASS_NAME,'link_login')
but it didn't work too. How can I make it to work?
You were close. To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CLASS_NAME:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "link_login"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.link_login"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='link_login']"))).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

How to locate the link of the tag I want from a tags with the same class with Selenium

I want to go to the jobs section on LinkedIn with selenium, but it leads to the connections(networks) section with the same class name. how do i solve this problem?
My code;
jobs_sec = driver.find_element("xpath", "//a[#class='app-aware-link global-nav__primary-link']")
jobs_sec.click()
The tag reached when run;
The desired element is a dynamic element, so to click on the clickable 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.app-aware-link.global-nav__primary-link[href^='https://www.linkedin.com/jobs']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='app-aware-link global-nav__primary-link' and starts-with(#href, 'https://www.linkedin.com/jobs')]"))).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
Try:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".app-aware-link.global-nav__primary-link")))
jobs_sec.click()
If that doesn't work, try as an alternative:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#data-nnt-old-href='https://www.linkedin.com/jobs/?']")))
jobs_sec.click()
You will also need to import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Unable to locate element with id, class_name and xpath using Selenium Python

How to click this button:
I have tried the following:
sumbitbutton = driver.find_element(By.XPATH, "//div[text() = 'mt8 mb8']")
I decided to insted abandon this method and go for a nuther as it lead to a lot of other problums latter down the line
You will want something like:
button = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "inputButton.main_submit")))
button.click()
As you did not confirm the URL, I cannot test it, but it should work.
Also, do not forget to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Generally <div> tags aren't clickable. Additionally mt8 mb8 are the classanmes, not the text.
Solution
As per the HTML:
to click on the <input> 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.inputButton.main_submit[value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='inputButton.main_submit' and #value='Submit']"))).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

How can I adjust this line of selenium code to get the status info of this item?

I have this line of code which I am trying to use to obtain the status of an item. Here is the line of code:
item_status = driver.findElement(By.className("status-info")).getText();
I'm not sure how I can adjust this to retrieve the text seen here:
from selenium import webdriver
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()
driver=webdriver.Chrome(options=options)
#Directing to site
driver.get("https://www.amazon.co.uk/Nintendo-Switch-OLED-Model-Neon/dp/B098TNW7NM/ref=sr_1_3?keywords=Nintendo+Switch&qid=1651147043&sr=8-3");
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/span/form/div[3]/span[1]/span/input"))).click()
When you are doing this
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/header/div/div[1]/div[2]/div/form/div[3]/div/span/input"))).click()
it will click on search icon, now on the result page, this xpath //span[#class='a-size-base a-color-success a-text-bold'] is not present hence nothing is getting printed on the console you are likely to face TimedoutException.
However looking at the screenshot that you've shared, I would say to use this xpath
//div[#id='availability']//span[contains(text(),'In stock.')]
If you want to print the text and tag
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerHTML"))
If only text you want:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerText"))
driver.findElement(By.className("status-info")) is the Java syntax and getText() is a Java method. Possibly you need Python syntax and method.
Solution
To print the text In stock. you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.a-size-base.a-color-success.a-text-bold"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='a-size-base a-color-success a-text-bold']"))).get_attribute("innerHTML"))
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

How to click on the webelement with in the highlighted script using Selenium and Python

I tried:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[#value='Sign Out']")))
but no luck.. please see image for the html script
The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and click():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form[action*='Logoff']>li>input[value='Sign Out']"))).click()
Using XPATH and submit():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(#action, 'Logoff')]/li/input[#value='Sign Out']"))).submit()
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

Categories