I can't import keys from Selenium - selenium

I got this error:
from selenium.webdriver.common.keys import keys
ImportError: cannot import name 'keys' from 'selenium.webdriver.common.keys' (C:\PYTHON\p10\venv\lib\site-packages\selenium\webdriver\common\keys.py).
I if I don't write this, it works:
from selenium.webdriver.common.keys import Keys
It opens Chrome and go to the site I want.
I don't know how can I solve the error to can find element and send keys to them.
I think the problem is that I can't import that "keys" and I don't know why.

you should write import 'Keys' correctly
from selenium.webdriver.common.keys import Keys
for send_keys() method, you don't need import anything.
The Keys class provide keys in the keyboard like RETURN, F1, ALT etc.
for more knowing about Keys use this Link

Related

To import WebDriverWait the correct way is to use selenium.webdriver.support.wait or selenium.webdriver.support.ui?

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
Since I started using Selenium I've always used ui and I've always seen others using ui too, but I'm not finding it in the Selenium documentation and now Pylance is showing this alert:
"WebDriverWait" is not exported from module "selenium.webdriver.support.ui"
Import from "selenium.webdriver.support.wait" instead
Both seem to work, but I didn't find anything warning about which one will become obsolete or anything like that. I would like to understand better what is behind this subject!
As per the official documentation, you have to use:
from selenium.webdriver.support.wait import WebDriverWait
Refer:
https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html

How to get the text from mouseover popup element using Selenium Webdriver [duplicate]

This question already has answers here:
How to handle HTML constraint validation pop-up using Selenium?
(3 answers)
Closed 3 months ago.
I am trying to get the text of the mouseover on the login page from the email field.
This is the site "https://app.involve.me/login/?_ga=2.49216998.1374332121.1660294616-36640509.1660294616"
If you will leave the fields empty and try to log in a popup will appear with the following message:
Please fill out this field
I cannot get the text from it. I tried as an alert, tooltip nothing works. There is no path to it.
Element snapshot:
The text of the mousehover on the login page from the email field which you are referring is the outcome of Constraint API's element.setCustomValidity() method.
Note: HTML5 Constraint validation doesn't remove the need for validation on the server side. Even though far fewer invalid form requests are to be expected, invalid ones can still be sent by non-compliant browsers (for instance, browsers without HTML5 and without JavaScript) or by bad guys trying to trick your web application. Therefore, like with HTML4, you need to also validate input constraints on the server side, in a way that is consistent with what is done on the client side.
Solution
To retrieve the text which results out from the element.setCustomValidity() method, you can use either of the following Locator Strategies:
Using Python and CssSelector:
Code Block:
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
driver.execute("get", {'url': 'https://app.involve.me/login/?_ga=2.49216998.1374332121.1660294616-36640509.1660294616'})
print(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).get_attribute("validationMessage"))
Console Output:
Please fill out this field.
Using Java and Xpath:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class validationmessage {
public static void main(String[] args) {
driver.get("https://app.involve.me/login/?_ga=2.49216998.1374332121.1660294616-36640509.1660294616");
System.out.println(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#name='email']"))).getAttribute("validationMessage"));
}
}
Console Output:
Please fill out this field.

How to use selenium and Python to click 'accept cookies' button in popup, how can I move on when popup has no x

I am new to using the selenium module. I have started with some simple tutorials which go ok until I get a popup.
Because the popup does not have an x, I am not able to apply other advice I have found online.
How to close pop up window in Selenium
However I have tried to inspect the code of the popup window and I can see that maybe I have a couple of options, close by referencing the link text 'Accept Cookies', or close by the button id which is "onetrust-accept-btn-handler"
This is the code I have so far.
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.reed.co.uk/jobs/senior-insight-analyst/42347955")
driver.implicitly_wait(10)
link = driver.find_element_by_link_text("onetrust-accept-btn-handler")
link.click()
Trying
link = driver.find_element_by_link_text("onetrust-accept-btn-handler")
and
link = driver.find_element_by_link_text("Accept cookies")
Result in errors
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"onetrust-accept-btn-handler"}
or
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Accept cookies"}
I am wondering if there is any better command than link text if the button id is known, or am I missing another step in the process because it is a pop up? Any help appreciated. Thank you.
Please use the explicit wait so that your popup window can come up and your selenium script can detect the element and click on it.
Use the below code -
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
Mark it as answer if it resolves your problem.
Thank you Swaroop Humane and Dev for your answers. The answer above works, I also needed to add in three more lines of import code before the solution managed to click the Accept cookies button in the pop up. https://selenium-python.readthedocs.io/waits.html
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
Final code below.
import selenium
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.reed.co.uk/jobs/senior-insight-analyst/42347955")
#driver.implicitly_wait(10)
#link = driver.find_element_by_link_text("onetrust-accept-btn-handler")
#link.click()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
Then this happened.
I probably need to figure out a way to run selenium while I am signed into this webservice, rather than let it run a new browser every time. Any ideas?

Click on an address dynamically populated in robot framework

I am inputting an address in a text box which dynamically displays an address (powered by google). I need to click the down arrow and hit tab to select an address. I tried Press Key but it did not work. So, I tried the below extended library. So, when in my test case, i call this in settings, Library ExtendedSelenium.py and in the Test case section, I called press_down_arrow. Still it does not press the down arrow. Am I doing wrong? Do I need to supply any ID or values? Kindly help
# ExtendedSelenium.py
from SeleniumLibrary import SeleniumLibrary
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class ExtendedSelenium(SeleniumLibrary):
def __init__(self):
super(ExtendedSelenium, self).__init__()
def press_down_arrow(self):
""" Emulates action "press down arrow on keyboard".
"""
ActionChains(self._current_browser()).send_keys(Keys.ARROW_DOWN, Keys.NULL).perform()

How to accept all the alerts of a website?

When browsing webpages, I get sometimes alerts. How can I accept each alert that appears ? I have no idea what is the number of alerts that are in a given webpage.
This link alerts, let us make a test:
from selenium import webdriver
from selenium.webdriver.common import alert
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
class AlertsManager:
def alertsManager(self,url):
self.url_to_visit=url
self.driver=webdriver.Firefox()
self.driver.get(self.url_to_visit)
try:
while WebDriverWait(self.driver,1).until(EC.alert_is_present()):
self.alert=self.driver.switch_to_alert()
self.driver.switch_to_alert().accept()
except TimeoutException:
pass
print("Continue what you want here ...")
if __name__=='__main__':
AM=AlertsManager()
url="http://htmlite.com/JS006.php"
AM.alertsManager(url)
You can set capabilities. However the required capabilities may not be implemented on some specific browsers, test it by your self (As my knowledge, it only works with FF).
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);