I'm using selenium. The source code of the element is:
<input tabindex="5" class="buttonStyle" onclick="submitForm('SEARCH','');return false" type="submit" value="Search">
The css shows
input[type='submit']
The code I use:
driver.find_element_by_xpath("//input[#type='submit']").click();
driver.find_element_by_css_selector("input[type='submit']").click()
Both don't work as expected.
The code below does not work as well:
driver.find_element_by_xpath("//a[#onclick='submitForm('SEARCH','');return false']").click()
InvalidSelectorException: Message: Unable to locate an element with the xpath expression //a[#onclick='submitForm('SEARCH','');return false'] because of the following error:
Error: Bad token, expected: ] got: SEARCH
I used the XPATH checker on internet explorer (the favourite page named MRI). It shows the target element. However the above code does not work.
I'm using Jupyter Notebook, Selenium and Internet Explorer (the page can only be opened in IE)
The error shows that the given XPATH is not valid. You seem to have an extra ']' in the xpath. Please update it.
wait = WebDriverWait(driver, 20)
button = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='submit']")))
button.click()
Add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Related
For the below line of code:
driver.find_element(By.CLASS_NAME,'list-card-heading')
I am getting below error:
NoSuchElementException Traceback (most recent call last)
Input In [23], in <cell line: 1>()
1 driver.find_element(By.CLASS_NAME,'list-card-heading')
Initially I had :
driver.find_element_by_class_name('list-card-heading')
but did some modifications in code by adding this below two lines:
from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME,'list-card-heading')
I was expecting to get below results:
<selenium.webdriver.remote.webelement.WebElement (session="945ae2a8da0536bea44330c4bbf0b24e", element="581cbcf9-7bb9-40dd-8601-4109cad55272")
But got this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".list-card-heading"}
Is it the driver issue? or selenium library issue.
This line of code:
driver.find_element_by_class_name('list-card-heading')
in selenium3 is equivalent to:
driver.find_element(By.CLASS_NAME,'list-card-heading')
using selenium4
Possibly the AUT(Application Under Test) changed or is a dynamic element, the reason you see the error.
This usecase
To identify a visible element ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "list-card-heading")))
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
A NoSuchElementException is thrown when the webdriver cannot find an elements that use the locator strategy it has been given.
While sometimes this happens because of a legitimately bad xpath, in my experience, this often happens because of a dom timing issue, and this can be caused by a variety of things, either in your code, or on the website's side of things.
Solution
I recommend using a WebDriverWait to make sure an element exists before using attempting to interact with it. Here is an example of what that might look like:
//Declare your driver as you normally would
WebDriverWait wait = WebDriverWait(driver, 10)
wait.until(len(driver.find_elements(By.CLASS_NAME,'list-card-heading')) != 0)
driver.find_element(By.CLASS_NAME,'list-card-heading')
Here is my code:
from selenium import webdriver
user = "someemail#email.com"
browser = webdriver.Chrome("/path/to/browser/")
browser.get("https://www.quora.com/")
username = browser.find_element_by_name("email")
browser.implicitly_wait(10)
username.send_keys(user)
Here is the error message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I think there is another thread with a similar issue. Either the solutions in that thread didn't work for me or I don't know how to implement the solutions.
find_element_by_name("email")
is present multiple times in DOM. So that wouldn't work.
You can try with this css selector :
input[class*='header_login_text_box'][name='email']
Code :
username = browser.find_element_by_css_selector("input[class*='header_login_text_box'][name='email']")
username.send_keys("user#gmail.com")
To send a character sequence to the Email field within Login section of Quora you need to induce WebDriverWait for the element to be clickable and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")
# options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.quora.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='title login_title' and text()='Login']//following::div[1]//input[#class='text header_login_text_box ignore_interaction']"))).send_keys("someemail#email.com")
Browser Snapshot:
As said in comment, the locator used returning two elements and required element is second one. driver trying to interact with first element, so exception is throwing.
good see in console, the locator returning required one or not.
> $$("[name='email']") (2) [input#__w2_wD9e9Qgz12_email.text, input#__w2_wD9e9Qgz18_email.text.header_login_text_box.ignore_interaction]
> 0: input#__w2_wD9e9Qgz12_email.text 1:
> input#__w2_wD9e9Qgz18_email.text.header_login_text_box.ignore_interaction
> length: 2
> __proto__: Array(0)
go for another locator, if not able to figure it out another locator, then comment, will help you.
from selenium import webdriver
user = "someemail#email.com"
browser = webdriver.Chrome("/path/to/browser/")
browser.get("https://www.quora.com/")
username = browser.find_element_by_xpath("//input[#class='text header_login_text_box ignore_interaction' and #type='text']")
browser.implicitly_wait(10)
username.send_keys(user)
Here You can find Why ElementNotInteractableException occurs.
If you are using the Select aproach like:
from selenium.webdriver.support.select import Select
try this
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '''//*[#id="ReportViewer1_ctl04_ctl07_ddValue"]''')))).select_by_visible_text(str(x))
The login process to https://login.microsoftonline.com was working fine until this weekend.
I was able to select the username by id and can pass the keys but the problem is with the password field.
This is the element i used for password
Driver.FindElementByXPath("//*[#id=\"passwordInput\"]");
I tried some other ways but they didn't work
Driver.FindElementByXPath("//*[#id=\"i0118\"]");
Driver.FindElementByXPath("//*[contains(text(), 'Password')]");
Here's the error that I got: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="passwordInput"]"}
(when I used id=passwordInput)
If you look at the HTML code of the site, you will see that the password field is
<input name="passwd" type="password" id="i0118" autocomplete="off" etc.>
so to target the element you can use input[type=password]
See code below for the implementation
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(service=Service('your_chromedriver_path'))
driver.get('https://login.microsoftonline.com/')
# sets a maximum waiting time for .find_element() and similar commands
driver.implicitly_wait(10)
driver.find_element(By.CSS_SELECTOR, 'input[type=email]').send_keys('some_email#gmail.com')
driver.find_element(By.CSS_SELECTOR, 'input[type=submit]').click()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[type=password]"))).send_keys('some_password')
I'm trying to use selenium to perform searches in lexisnexis and I can't get it to find the search box.
I've tried find_element_by using all possible attributes and I only get the "NoSuchElementException: Message: no such element: Unable to locate element: " error every time.
See screenshot of the inspection tab -- the highlighted part is the element I need
My code:
from selenium import webdriver
import numpy as np
import pandas as pd
searchTerms = r'something'
url = r'https://www.lexisnexis.com/uk/legal/news' # this is the page after login - not including the code for login here.
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
browser.get(url)
I tried everything:
browser.find_element_by_id('search-query')
browser.find_element_by_xpath('//*[#id="search-query"]')
browser.find_element_by_xpath('/html/body/div/header/div/form/div[2]/input')
etc..
Nothing works. Any suggestions?
Could be possible your site is taking to long to load , in such cases you can use waits to avoid synchronization issue.
wait = WebDriverWait(driver, 10)
inputBox = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='search-query']")))
Note : Add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
I currently have a selenium python script that is working with PhantomJS, but not Firefox. I get this error:
NoSuchElementException('Unable to locate element: Research Project', None, None)
Specifically, it fails on this line of code:
self.webdriver.find_element_by_link_text("Research Project").click()
I've tried some different wait methods with no luck, such as:
WebDriverWait(self.webdriver, 10000).until(EC.presence_of_element_located((By.LINK_TEXT, "Research Project")))
I've also tried using various Xpaths to test if the link text isn't specific enough, which resulted in an ElementNotInteractableException if no wait was implemented, or the wait timing out if implemented.
If it helps, my PhantomJS webdriver is defined as:
args = [
'--ignore-ssl-errors=true',
'--ssl-protocol=any',
'--web-security=false'
]
driver = webdriver.PhantomJS(service_args=args, executable_path='/usr/bin/phantomjs', service_log_path='/tmp/ghostdriver.log')
And my Firefox webdriver is simply defined as:
driver = webdriver.Firefox(executable_path='/usr/local/Cellar/geckodriver/0.21.0/bin/geckodriver')
I can provide more detail if necessary. Any help would be greatly appreciated.
The HTML for the object I'm trying to reference:
<div id="dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:closeTree" style="padding-left:20px;" class="unselectedDiv"><img src="/converis/javax.faces.resource/images/collapsible_panel_triangle_state_expanded.png.xhtml?ln=intern" alt=""><span style="padding-left:15px;">Research Project </span></div><span id="dynaTempSelView:dynaTempSelForm:tree_selector:newLinksDT:0:hiddenDescriptiontree_closed" style="display: none;">Research Project</span>
The relevant HTML would have been helpful to decide on the Locator Strategy. Still, as per your code trials, moving forward as you are trying to invoke click() method so instead of using presence_of_element_located() you need to use element_to_be_clickable() as follows:
LINK_TEXT:
WebDriverWait(self.webdriver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Research Project"))).click()
PARTIAL_LINK_TEXT:
WebDriverWait(self.webdriver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Research Project"))).click()
XPATH:
WebDriverWait(self.webdriver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Research Project')]"))).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