javascript error: Right-hand side of 'instanceof' is not an object error using expected_conditions with Selenium Python - selenium

I have some code in selenium/python, which used to work, But now I'm getting:
javascript error: Right-hand side of 'instanceof' is not an object
I don't know if it's something that was changed in the angular application, or how this error is relevant to what I'm doing.
I get it after trying to locate a webelement or waiting for visibility of element, like:
wait.until(expected_conditions.visibility_of_element_located(leftNav_page.pageSmallTitle))
"pageSmallTitle" is just a locator like:
pageSmallTitle = (By.CSS_SELECTOR, "span[id$='componentTitle']")
Tried changing it to xpath, but didn't make a difference.
I would highly appreciate any suggestion!

visibility_of_element_located() accepts a locator as an argument and the expression is as follows:
wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "span[id$='componentTitle']")))
In your usecase, if:
pageSmallTitle = (By.CSS_SELECTOR, "span[id$='componentTitle']")
the effective line of code must have been:
from selenium.webdriver.support import expected_conditions
wait.until(expected_conditions.visibility_of_element_located(pageSmallTitle))
so essentially there is an additional parameter as leftNav_page. Hence you see the error.
Removing the extra parameter will solve the issue.

Related

AttributeError: 'WebDriver' object has no attribute 'find_element_by_id' - Selenium Webdriver [duplicate]

When starting the function
def run(driver_path):
driver = webdriver.Chrome(executable_path=driver_path)
driver.get('https://tproger.ru/quiz/real-programmer/')
button = driver.find_element_by_class_name("quiz_button")
button.click()
run(driver_path)
I'm getting errors like these:
<ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=driver_path)
<ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
button = driver.find_element_by_class_name("quiz_button")
... but I can't understand why.
I'm using WebDriver at the latest version for my Chrome's version. I don't why I get
find_element_by_* commands are deprecated
... when it's in the documentation that the command exists.
This error message...
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
...implies that the find_element_by_* commands are deprecated in the latest Selenium Python libraries.
As AutomatedTester mentions: This DeprecationWarning was the reflection of the changes made with respect to the decision to simplify the APIs across the languages and this does that.
Solution
Instead you have to use find_element(). As an example:
You have to include the following imports
from selenium.webdriver.common.by import By
Using class_name:
button = driver.find_element_by_class_name("quiz_button")
Needs be replaced with:
button = driver.find_element(By.CLASS_NAME, "quiz_button")
Along the lines of, you also have to change the following:
Using id:
element = find_element_by_id("element_id")
Needs be replaced with:
element = driver.find_element(By.ID, "element_id")
Using name:
element = find_element_by_name("element_name")
Needs be replaced with:
element = driver.find_element(By.NAME, "element_name")
Using link_text:
element = find_element_by_link_text("element_link_text")
Needs be replaced with:
element = driver.find_element(By.LINK_TEXT, "element_link_text")
Using partial_link_text:
element = find_element_by_partial_link_text("element_partial_link_text")
Needs be replaced with:
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
Using tag_name:
element = find_element_by_tag_name("element_tag_name")
Needs be replaced with:
element = driver.find_element(By.TAG_NAME, "element_tag_name")
Using css_selector:
element = find_element_by_css_selector("element_css_selector")
Needs be replaced with:
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
Using xpath:
element = find_element_by_xpath("element_xpath")
Needs be replaced with:
element = driver.find_element(By.XPATH, "element_xpath")
Note: If you are searching and replacing to implement the above changes, you will need to do the same thing for find_elements_*, i.e., the plural forms of find_element_*.
You may also find this upgrade guide useful as it covers some other unrelated changes you may need to make when upgrading: Upgrade to Selenium 4
#DebanjanB mentioned and explained the new structure. Also, it's better use these lines:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
s = Service('C:/Users/.../chromedriver.exe')
driver = webdriver.Chrome(service=s)
As others mentioned, you should use find_element() or find_elements() instead of find_element_by_*() or find_elements_by_*().
I wrote the regex pattern to replace the deprecated methods to new ones, so try this if you need.
# from - e.g. find_element_by_id("test")
find_element(s?)_by_([a-z]+)\((.*)
# to - e.g. find_element(By.ID, "test")
find_element$1(By.\U$2\E, $3
Note: you need the import line to use the new methods
from selenium.webdriver.common.by import By
Thank you #Stephen and undetected Selenium for your answers. After some time reading on how where to find an example of send_key, I found an amazing gist of examples.
The send_keys below example worked for me:
browser = webdriver.Chrome()
 
def test_key_down(driver):
driver.get('https://www.selenium.dev/selenium/web/single_text_input.html?#')
ActionChains(driver) \
.send_keys("abc") \
.perform()
 
test_key_down(browser)

NoSuchElementException using driver.find_element(By.CLASS_NAME,'list-card-heading') in Selenium

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

Duration cannot be resolved or is not a field in WebDriverwait in Selenium

I am trying to interact with a calendar on a webpage, so I am using WebDriver wait.
Below is the method:
new WebDriverWait(driver,Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className("calendar")));
Is something wrong here?
The errors are shown as:
Multiple markers at this line
- Duration cannot be resolved or is not a field
- Syntax error on token ",", . expected
- The constructor WebDriverWait(WebDriver, Duration) is
undefined
what is wrong here?
To resolve the Duration issue :
import java.time.Duration;
import this at the top, "Duration" got resolved it worked for me.

sending keys to a difficult element in selenium (Python)

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

NoSuchElementException('Unable to locate element: Research Project', None, None) while locating element through linkText

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