How to print messages in console in Python Selenium - python

I'm using below code to test login to a website.The code opens a webpage and wait for an element to load and then logs in using the provided credentials. I have put some messages in the code for me to identify whether the code is running. However, it is not working.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
s = Service("/usr/local/bin/chromedriver")
url = "https://atf.domain.com/"
driver = webdriver.Chrome(options=options, service=s)
driver.get(url)
print(driver.title)
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
print("Page loaded!")
)
except TimeoutException:
print("Page not loaded!")
driver.find_element(By.ID, "username").send_keys("email#gmail.com")
driver.find_element(By.ID, "password").send_keys("password")
driver.find_element(By.ID, "signinButton").click()
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "compose-delivery-link"))
print("Compose delivery page loaded!")
)
except TimeoutException:
print("Compose delivery page not loaded!")
driver.find_element(By.ID, "compose-delivery-link").click()
driver.close()
I'm getting below error,
File "sft_login_test.py", line 24
print('Page loaded!')
^
SyntaxError: invalid syntax
I have no tried idea how to fix this. Any help is greatly appreciated.

element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
print("Page loaded!")
I guess the print call should be outside the .until arguments.

You accidentally wrote a call to print() inside the parentheses of .until()
Both at line 23
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
print("Page loaded!")
)
and at line 37
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "compose-delivery-link"))
print("Compose delivery page loaded!")
)
Additionally, you're gonna want to import the TimeoutException from selenium.common.exceptions, because that's likely the next error you will encounter.
And to give some side Information, WebDriverWait doesn't actually wait 10 seconds before looking for the element. It rather checks for the condition (here the presence of an element with ID "name" or "password" respectively) in a set interval until it either finds the element, in which case the element is returned, or the time limit (here 10 seconds) is reached, in which case it throws a TimeoutException.

Related

Unable to locate the element on an angular website

I have tried the below code and it is always timing out. But when I look it up with the browser inspector, I can see the Username input element.
I have also tried to find it by ID but not able to.
Tried almost all of the existing questions/solutions on this forum but was unable to figure it out.
driver.get("https://www.dat.com/login")
time.sleep(5)
driver.find_element_by_css_selector("a[href*='https://power.dat.com/']").click()
time.sleep(5)
# explicitly waiting until input element load
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "username"))
)
except TimeoutException:
print(
"Waited 10 seconds for the username input to load but did not happen..."
)
except Exception as e:
print(f"Exception while waiting for username input to appear: \n {e}")
sys.exit(1)
2 issues here:
After clicking on "DAT Power" on the first page a new tab is opened. To continue working there you need to switch the driver to the second tab.
You will probably want to enter a text into the username there. If so you need to wait for element clickability, not presence only.
Also, the current Selenium version (4.x) no more supports find_element_by_* methods, the new style should be used, as following presented.
The following code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.dat.com/login"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='https://power.dat.com/']"))).click()
new_tab = driver.window_handles[1]
driver.switch_to.window(new_tab)
wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys("name#gmail.com")
The result is

Chromedriver selenium python

i am working on a python project using selenium.
I get the following error and would like some help please
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot determine loading status
from unknown error: unexpected command response
(Session info: chrome=103.0.5060.114)
My chrome is version 103, and so is my chrome driver.
Here is the code
from selenium.webdriver.common.keys import Keys
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.webdriver.chrome.service import Service
player = input("Which player: ").lower()
PATH = "/Users/makfadel/Desktop/Desktop-items/chromedriver 2"
#driver = webdriver.Chrome(PATH)
s = Service(PATH)
browser = webdriver.Chrome(service=s)
browser.get("https://www.basketball-reference.com/leagues/NBA_2022_per_game.html")
search = browser.find_element(By.XPATH, "//*[#id='header']/div[3]/form/div/div/input[2]")
search.send_keys(player)
search.send_keys(Keys.RETURN)
try:
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='players']/div[1]/div[1]/strong/a")))
element.click()
name = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='meta']/div[2]/h1/span")))
print()
print(name.text)
print()
szn = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[1]/div/p[1]/strong"))
)
games = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[1]/p[1]"))
)
ppgame = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[2]/p[1]"))
)
rebounds = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[3]/p[1]"))
)
assists = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[2]/div[4]/p[1]"))
)
fg = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[3]/div[1]/p[1]"))
)
fg3 = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[3]/div[2]/p[1]"))
)
ft = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='info']/div[4]/div[3]/div[3]/p[1]"))
)
print(f"Season: {szn}")
print(f"Games played: {games.text}")
print(f"Points per game: {ppgame.text}")
print(f"Rebounds per game: {rebounds.text}")
print(f"Assists per game: {assists.text}")
print(f"Field goal percentage: {fg.text}%")
print(f"Field goal from 3 percentage: {fg3.text}%")
print(f"Free throw percentage: {ft.text}%")
except Exception:
print(f"No player named {player}")
finally:
browser.quit()
There has been an issue with chrome driver 103 version
Please find below the bug ids for the same,
https://bugs.chromium.org/p/chromedriver/issues/detail?id=4121&q=label%3AMerge-Request-103
Check it on GitHub Selenium Issue - https://github.com/SeleniumHQ/selenium/issues/10799
Solution
For now, until this issue is fixed try to "Downgrade Chrome Browser To v102" and "Download Selenium Chrome Driver 102" and try to run your script, as this issue is happening in 103 version.
Upgrade the chromedriver to v104
sample code snippet -
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
// 1
option = Options()
option.binary_location='/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta'
// 2
driver = webdriver.Chrome(service=Service(ChromeDriverManager(version='104.0.5112.20').install()), options=option)
code reference - https://www.globalnerdy.com/2022/06/30/fix-the-chromedriver-103-bug-with-chromedriver-104/

Element not clickable, ElementClickInterceptedException in Selenium

A similar question has been asked many times, and I have gone over many of them, such as Debugging "Element is not clickable at point" error
, Selenium Webdriver - element not clickable error in firefox, ElementClickInterceptedException: Message: element click intercepted:
but haven't been able to solve my problem.
I want to select a subset of car brands from the websites search dropdown menu. Usually I would do it via Selenium's Select, but that doesn't do the trick here.
Here's my code.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
ser = Service(executable_path= r'D:\chromedriver.exe')
#Note I have omitted the options that I use (proxy and header).
driver = webdriver.Chrome(service = ser)
driver.get("https://www.autotalli.com/")
time.sleep(5)
# Accepting cookies
driver.find_element(by = By.XPATH, value = "//button[contains(text(),'Asetuks')]").click()
time.sleep(5)
driver.find_element(by = By.XPATH, value = "//button[contains(text(),'Tallenna')]").click()
driver.maximize_window()
time.sleep(5)
#selecting parameters from the dropdown menu
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#class = 'mbsc-input-wrap']")))
element.click()
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#data-val = '66-duplicated']")))
element.click()
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#class = 'mbsc-input-wrap']")))
element.click()
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#data-val = '10-duplicated']")))
element.click()
What throws me off is that the code works for the 66-duplicated element but not for the 10-duplicated element, and the two are identical in every way. The error I get is
Exception has occurred: ElementClickInterceptedException
Message: element click intercepted: Element <div role="option" tabindex="-1" aria-selected="false" class="mbsc-sc-itm mbsc-sel-gr-itm mbsc-btn-e" data-index="2" data-val="10-duplicated" style="height:40px;line-height:40px;">...</div> is not clickable at point (268, 217). Other element would receive the click: <input tabindex="0" type="text" class="mbsc-sel-filter-input mbsc-control" placeholder="Hae">
To to solve this, I have tried to use javascript, move to the element and then click and maximize the window - None of which worked.
#Attempt 1:js:
driver.execute_script("arguments[0].click()", element)
#Attempt 2: moveToElement:
element = driver.find_element(by = By.XPATH, value = "//*[#data-val = '10-duplicated']")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#data-val = '10-duplicated']")))
element.click()
I have also tried a combination of these but to no avail.
However,when I put a break point right before the click of element "10-duplicated" and manually scroll and move the mouse to the element, and run the remaining code, it works.
I am quite puzzled here. What's going on and how can this problem be solved?
There are 17 matches for //*[#class = 'mbsc-input-wrap'] locator on that page, but you are opening the same, first match 2 times. That is Merkit droplist.
Now, when selecting //*[#data-val = '66-duplicated'] (Nissan) from the opened droplist this will work since that option is within the visible options but when Nissan is currently selected //*[#data-val = '10-duplicated'] (BMW) option in not visible, you can not click it directly.
In order to select it now you will have to
Cancel the previous selection of Nissan so that BMW will become initially visible by opening the droplist.
Scroll the droplist
Click the //*[#data-val = '10-duplicated'] with JavaScript - not recommended since this is not what human user can do via GUI.
I will give you a code to make the first approach - cancelling the previous Nissan selection.
I have also made some improvements there.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
ser = Service(executable_path= r'D:\chromedriver.exe')
#Note I have omitted the options that I use (proxy and header).
driver = webdriver.Chrome(service = ser)
driver.get("https://www.autotalli.com/")
wait = WebDriverWait(driver, 20)
time.sleep(5)
# Accepting cookies
driver.find_element(by = By.XPATH, value = "//button[contains(text(),'Asetuks')]").click()
time.sleep(5)
driver.find_element(by = By.XPATH, value = "//button[contains(text(),'Tallenna')]").click()
driver.maximize_window()
time.sleep(5)
#selecting parameters from the dropdown menu
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#class = 'mbsc-input-wrap']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#data-val = '66-duplicated']"))).click()
#clear the previously selected NIssan option
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(#class,'usedCarsMakeClear clearOption')]"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#class = 'mbsc-input-wrap']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#data-val = '10-duplicated']"))).click()

Selenium: different behaviour debugging versus running. Wrong WebDriverWait?

Following code runs as expected if manually stepped in debug while it misbehaves if just run.
Aside from some initial code to reach the search page, I am then requesting a value which I know does not exist ("ZQZZQ") and thus the code should branch into the last else statement.
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
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(
"/Users/bob/Documents/work/AIFA/scraper/scrape_gu/chromedriver"
)
# navigate through the initial agreement screens
driver.get("https://farmaci.agenziafarmaco.gov.it/bancadatifarmaci/cerca-farmaco")
readunderstood = driver.find_element_by_id("conf")
readunderstood.click()
accept = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[5]/div[3]/div/button"))
)
accept.click()
# end of the initial agreement screens and general preparation
##############################################################
string_to_search = "ZQZZQ" # we can safely assume this does not exist
find_textbox = driver.find_element_by_id("search")
find_textbox.clear() # after the first search the old value will still be there
find_textbox.send_keys(string_to_search)
find_textbox.send_keys(Keys.ENTER)
# check if any results found
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "noresultsfound"))
)
# if we did retrieve something we don't have an error message
if element.text != "Nessun risultato trovato":
# first we have to find out the total number of retrieved pages
print(f"Digram {string_to_search} found!")
else:
print(f"Digram {string_to_search} not found. Need to close modal window!")
# click on ok of name not found modal dialog
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[4]/div[3]/div/button"))
).click()
print("Modal window hopefully closed")
When I run it at full speed though I find it goes into the "if" branch, printing "Digram ZQZZQ found!" which is wrong.
Is the WebDriverWait call wrong? Thank you
You need to change the "presence_of_element_located" method to "visibility_of_element_located".
Old Code:
# check if any results found
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "noresultsfound"))
Change:
element = WebDriverWait(driver, 5).until(
EC.visibility_of_element_located((By.XPATH, "//*[#id='noresultsfound']"))
)
Output:
Digram ZQZZQ not found. Need to close modal window!
Modal window hopefully closed
VisibilityOfElementLocated Vs presenceOfElementLocated:
https://stackoverflow.com/questions/38038920/visibilityofelementlocated-vs-presenceofelementlocated#:~:text=while%20the%20visibilityOfElementLocated%20has%20to,of%20presenceOfElementLocated%20will%20be%20enough.

Selenium isnt finding elements correctly

i want to log in to the Instagram page. I want to press the "Jetzt nicht" button, but my Selenium cant find it, no matter if im searching for the class name, or the tag name...
Could somebody please help me?
Btw: the code will be edited later :)
[see the class name etc]
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
#define webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.instagram.com/")
#wait 10s or till cookies loaded and accept
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "button")))
element.click()
#type username account, wait max 10s until searchbar loaded
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "username")))
element.send_keys("yeet")
#hit enter
element.send_keys(Keys.RETURN)
#type password
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "password")))
element.send_keys("yeet")
#hit enter
element.send_keys(Keys.RETURN)
#press the login button, press enter
element.send_keys(Keys.RETURN)
#click false on safe ur login information
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "B.sqdOP yWX7d y3zKF ")))
element.click()
sleep(30)
#quit
driver.quit()
driver.close()
(By.CLASS_NAME, "B.sqdOP yWX7d y3zKF ") locator is wrong!
By.CLASS_NAME locator receives single class name only, not multiple class names.
Also the long spaces between class names here absolutely not correct.
Also the class names you are using here seems to be dynamically changing, not something fixed.
And the B at the beginning looks to be absolutely irrelevant...
If you are using English version of instagram.com and wants to click on Not Now button which just appear once you login successfully. you can use the below code :
try:
# click false on save your login information
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Not Now']")))
element.click()
except:
print('something went wrong')
pass
should work for you.

Categories