Why does my code:
self.driver.find_element_by_xpath("//*[text()[contains(.,'%s')]]" % ('Sorry'))
Get stuck and won't pass this line? Even if I do something like:
driver.implicitly_wait(30)
self.driver.find_element_by_xpath("//*[text()[contains(.,'%s')]]" % ('Sorry'))
Complete code:
# gets stuck here
if self.is_text_present('Hello'):
print 'Hello'
# rest of code
def is_text_present(self, text):
try:
self.driver.find_element_by_xpath('//*[contains(text(), "%s")]' % (text))
except NoSuchElementException, e:
return False
return True
Your XPath can be simplified to
"//*[contains(text(),'%s')]" % ('Sorry')
Perhaps try something like:
import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui
with contextlib.closing(webdriver.Firefox()) as driver:
...
# Set up a WebDriverWait instance that will poll for up to 10 seconds
wait = ui.WebDriverWait(driver, 10)
# wait.until returns the value of the callback
elt = wait.until(
lambda driver: driver.find_element_by_xpath(
"//*[contains(text(),'%s')]" % ('Sorry')
))
From the docs:
This waits up to 10 seconds before throwing a TimeoutException or if
it finds the element will return it in 0 - 10 seconds.
To debug the problem you might try saving the HTML source to a file right before calling find_element_by_xpath so you can see what the driver is seeing. Is the XPath valid for that HTML?.
def is_text_present(self, text):
with open('/tmp/debug.html', 'w') as f:
time.sleep(5) # crude, but perhaps effective enough for debugging here.
f.write(driver.page_source)
try:
self.driver.find_element_by_xpath('//*[contains(text(), "%s")]' % (text))
except NoSuchElementException, e:
return False
return True
Related
Ok this is a bit embarrassing because I've asked a similar question on here sometime ago, but I tried the suggested solution ie (wait till element clickable), but it didn't work. So here's my code snipped.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import ElementClickInterceptedException, ElementNotInteractableException, TimeoutException, WebDriverException, NoSuchElementException
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
import re
import pandas as pd
def get_links(link):
driver = webdriver.Firefox()
driver.get(link)
driver.implicitly_wait(50)
sleep(5)
_flag = True
knt = 0
while _flag:
try:
WebDriverWait(driver, 50).until(EC.invisibility_of_element((By.XPATH, "//a[contains(class='ot-buttons-fw')]")))
WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='event__more event__more--static']")))
driver.find_element_by_xpath("//*[contains(text(), 'Show more matches')]").click()
print("works here!")
print("clicked....................................")
sleep(5)
_flag = True
#tmp = driver.find_elements_by_xpath("//span[contains(text(), 'NBA - Pre-season')]")
#if len(tmp) > 0:
#print("Found them!")
#_flag = False
if knt > 5: # For testing
print("Nuff clicked")
_flag = False
except(ElementNotInteractableException):
print("Error!")
_flag = False
driver.close()
return None
link = "https://www.flashscore.com/basketball/usa/nba/results/"
_ = get_links(link)
For some reason I keep getting an ElementClickInterceptedException Error at the driver.find_element_by_xpath("//*[contains(text(), 'Show more matches')]").click() line. Any help can do please
Your element overlap with other element, it cause the ElementClickInterceptedException error appear.
Before perform you code, please close the cookies popup with this code snippet:
def get_links(link):
driver = webdriver.Firefox()
driver.get(link)
driver.implicitly_wait(50)
sleep(5)
#here, close popup
if(len(driver.find_elements_by_id('onetrust-accept-btn-handler'))>0):
driver.find_element_by_id('onetrust-accept-btn-handler').click()
_flag = True
knt = 0
while _flag:
....
....
And remove this line:
WebDriverWait(driver, 50).until(EC.invisibility_of_element((By.XPATH, "//a[contains(class='ot-buttons-fw')]")))
This is invalid xpath expression, and no needed, have handled by if(popup accept) condition the above.
I need to web scrape data from a trading view chart using an infinite loop, but I keep running into this error - StaleElementReferenceException.
I have tried making the program wait explicitly using the following function -
exceptions = (NoSuchElementException, StaleElementReferenceException)
def locate(path, type="xpath", time=5):
global chrome
global exceptions
if type == "xpath":
element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
expected_conditions.presence_of_element_located((By.XPATH, path))
)
if type == "link_text":
element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
expected_conditions.presence_of_element_located((By.LINK_TEXT, path))
)
if type == "name":
element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
expected_conditions.presence_of_element_located((By.NAME, path))
)
return element
Here is the full code that I have written:
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = "D:\\Repositories\\Bot\\chromedriver v89.0.4389.23.exe"
exceptions = (NoSuchElementException, StaleElementReferenceException)
def locate(path, type="xpath", time=5):
global chrome
global exceptions
if type == "xpath":
element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
expected_conditions.presence_of_element_located((By.XPATH, path))
)
if type == "link_text":
element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
expected_conditions.presence_of_element_located((By.LINK_TEXT, path))
)
if type == "name":
element = WebDriverWait(chrome, time, ignored_exceptions=exceptions).until(
expected_conditions.presence_of_element_located((By.NAME, path))
)
return element
def login():
global chrome
chrome.get("https://in.tradingview.com/chart/iVucV9D0/")
chrome.maximize_window()
locate("Sign in", "link_text").click()
locate(
"/html/body/div[11]/div/div[2]/div/div/div/div/div/div/div[1]/div[4]/div/span"
).click()
locate("username", "name").send_keys("myemail")
locate("password", "name").send_keys("mypassword" + Keys.ENTER)
time.sleep(3)
locate("/html/body/div[6]/div/div/div[2]/div/div/div[1]/div[2]/form/button").click()
def buy():
buyprice = locate(
"/html/body/div[2]/div[5]/div/div[1]/div[1]/div[5]/div/div[2]/div[1]/div[3]/div[2]/div[3]/div[2]/span"
).text
if buyprice != "n/a":
return float(buyprice)
else:
return "na"
def sell():
sellprice = locate(
"/html/body/div[2]/div[5]/div/div[1]/div[1]/div[5]/div/div[2]/div[1]/div[3]/div[2]/div[6]/div[2]/span"
).text
if sellprice != "n/a":
return float(sellprice)
else:
return "na"
with webdriver.Chrome(driver) as chrome:
login()
while True:
if buy() != "na":
print("Supertrend Buy detected")
# execute rest of the code
if sell() != "na":
print("Supertrend Sell Detected")
# execute rest of the code
Can someone please help me?
PS: I am using python 3.9 and selenium version 3.141.0
I've found that a lot of these weird issues can be solved simply by downgrading to an older chromedriver and/or chrome/chromium, or switching to firefox/geckodriver. The range of compatibility between Selenium, the webdriver, and the browser is very narrow and unforgiving.
Just to preface, I've only barely started messing with python, and have no grasp on many python-specific concepts.
I'm refactoring some code and I'm trying to hide my janky error handling for getting selenium web elements, and I've come across a strange problem when passing elements around:
selenium_handler.py:
import time
from selenium.webdriver.chrome.webdriver import WebDriver
import selenium
def highlight(element):
"""Highlights (blinks) a Selenium Webdriver element"""
driver = element._parent
def apply_style(s):
driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",
element, s)
original_style = element.get_attribute('style')
apply_style("background: yellow; border: 2px solid red;")
time.sleep(.3)
apply_style(original_style)
def find_element(driver: WebDriver, xpath: str, url: str = "", test: bool = True, max_timeout: int = -1,
max_attempts: int = -1, time_between_attempts: int = 0) -> selenium.webdriver.remote.webelement.WebElement:
if url:
driver.get(url)
start_time = time.time()
end_time = start_time + max_timeout
attempt_count = 0
element: selenium.webdriver.remote.webelement.WebElement
while time.time() < end_time if max_timeout >= 0 else True and \
attempt_count < max_attempts if max_attempts >= 0 else True:
attempt_count += 1
try:
element = driver.find_element_by_xpath(xpath)
if test:
print("highlighting")
highlight(element)
# element.click()
break
except:
time.sleep(time_between_attempts)
pass
# should only be past here if something went wrong so send error
element.click()
return element
raise Exception("could not get element")
main.py:
from selenium import webdriver
from real_project import selenium_handler
import time
driver = webdriver.Chrome()
# -------------------------------Cape Coral----------------------
cape_coral_url = "https://capegis.maps.arcgis.com/apps/webappviewer/index.html?id=bbce9a471a534e7482d35716c4cb6f36"
time_for_elements_to_be_found = 5
def prepare_for_batch():
driver.refresh()
ok_button = selenium_handler.find_element(driver, url=cape_coral_url,
xpath="html/body/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/button",
# test=False,
max_timeout=time_for_elements_to_be_found)
print(type(ok_button))
while True:
try:
ok_button.click()
break
except:
pass
prepare_for_batch()
time.sleep(5)
driver.close()
My error shows up on element.click(). As it is, I will get an error that the element is not interactable. But if I instead use the element.click() inside the try catch block then it will always work, no problem.
If I run the bottom element.click() in a while-try-break-except loop like the first then it will eventually click.
Why is this happening? It doesn't look like it's a race condition or that html is still loading up or anything like that or else the first element.click() would fail. I just want to be able to pass these elements around in my code. Thanks
EDIT:
forgot the code actually calling this function
This is my code:
from selenium import webdriver
proxylist=['58.12.12.12:80','69.12.12.12:80']
weblist=['https://www.google.com','https://www.facebook.com','https://www.yahoo.com','https://aol.com']
for s in range (len(proxylist)):
service_args = ['--proxy=%s'%(proxylist[s]),'--proxy-type=socks5']
driver = webdriver.PhantomJS('phantomjs.exe', service_args = service_args)
for s in weblist:
driver.get(s)
The idea is the browser first will use proxylist[0] to go to those sites. If proxylist[0] is timeout at website[2] then proxylist[1] will continue to do the job with website[3]. I think i should use try and except but a don't know where to put them. Glad you helped!
Try something like this. Basicaly we are switching the inner and the outer loops and adding a try/except
for s in weblist:
for s in range (len(proxylist)):
try
service_args = ['--proxy=%s'%(proxylist[s]),'--proxy-type=socks5']
driver = webdriver.PhantomJS('phantomjs.exe', service_args = service_args)
driver.get(s)
break
except TimeOutException:
print 'timed out'
The try catch for time out was something like:
try:
driver.set_page_load_timeout(1)
driver.get("http://www.example.com")
except TimeoutException as ex:
print("Exception has been thrown. " + str(ex))
For your code, adding it would be something like:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
proxylist=['58.12.12.12:80','69.12.12.12:80']
weblist=['https://www.google.com','https://www.facebook.com','https://www.yahoo.com','https://aol.com']
def test():
temp_count_proxy = 0
driver_opened = 0
for url in weblist:
if temp_count_proxy > len(proxylist):
print("Out of proxy")
return
if driver_opened == 0:
service_args = ['--proxy={}'.format(proxylist[temp_count_proxy]),'--proxy-type=socks5']
driver = webdriver.PhantomJS('phantomjs.exe', service_args = service_args)
driver_opened = 1
try:
driver.set_page_load_timeout(2)
driver.get(url)
except TimeoutException as ex:
driver.close()
driver_opened = 0
temp_count_proxy += 1
continue
test()
Just becareful, as if it fail to get one url, it will change proxy, and get the next url (as you requested) but not get the same url.
if you want it to change proxy when fail the retry with the current url , use following:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
proxylist=['58.12.12.12:80','69.12.12.12:80']
weblist=['https://www.google.com','https://www.facebook.com','https://www.yahoo.com','https://aol.com']
def test():
temp_count_proxy = 0
driver_opened = 0
for url in weblist:
while True:
if temp_count_proxy > len(proxylist):
print("Out of proxy")
return
if driver_opened == 0:
service_args = ['--proxy={}'.format(proxylist[temp_count_proxy]),'--proxy-type=socks5']
driver = webdriver.PhantomJS('phantomjs.exe', service_args = service_args)
driver_opened = 1
try:
driver.set_page_load_timeout(2)
driver.get(url)
# Your code to process here
except TimeoutException as ex:
driver.close()
driver_opened = 0
temp_count_proxy += 1
continue
break
I have exported a test case and saved as .py file. I want to edit it to read data from a text file, as inputs for logging into the website that I am testing. The sample username and password that should allow a login is, 1 and pwd1 respectively. But when I read these values from a file by accessing the first line read , using readlines(), It shows failure while logging in and the test case fails. When I printed the size of the corresponding line read by readlines, it's size is 1 more than it should be. i.e. size=2 whereas size of my username, 1, should be 1 I assume. Need help in making this test case a success. Here is my code:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import time
class A6Test2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://localhost:8080"
self.verificationErrors = []
self.accept_next_alert = True
def test_a6_test2(self):
fh = open("cases.txt", "r")
data=fh.readlines()
uname1=data[1]
pwd1=data[2]
driver = self.driver
driver.get(self.base_url + data[0])
driver.find_element_by_name("p_id").clear()
driver.find_element_by_name("p_id").send_keys(data[1])
driver.find_element_by_name("p_passw").clear()
driver.find_element_by_name("p_passw").send_keys(data[2])
time.sleep(5)
driver.find_element_by_xpath("(//input[#value='Submit'])[2]").click()
time.sleep(3)
driver.find_element_by_css_selector("input[type=\"SUBMIT\"]").click()
driver.find_element_by_link_text(data[5]).click()
driver.get(self.base_url + data[6])
time.sleep(3)
driver.find_element_by_name("p_id").clear()
driver.find_element_by_name("p_id").send_keys(data[7])
time.sleep(3)
driver.find_element_by_name("p_passw").clear()
driver.find_element_by_name("p_passw").send_keys(data[8])
time.sleep(3)
driver.find_element_by_xpath("(//input[#value='Submit'])[2]").click()
time.sleep(3)
driver.find_element_by_css_selector("input[type=\"SUBMIT\"]").click()
driver.find_element_by_link_text(data[11]).click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
My cases.txt is as follows:
/source.jsp
1
pwd1
(//input[#value='Submit'])[2]
input[type=\"SUBMIT\"]
Log out
/source.jsp
1
pwd1
(//input[#value='Submit'])[2]
input[type=\"SUBMIT\"]
Log out
data can be seen as a 2D array. Try accessing it using 2 indices For Example:
driver.get(self.base_url + "/source.jsp")
driver.find_element_by_name("d_id").clear()
driver.find_element_by_name("d_id").send_keys(data[0][0:3])
driver.find_element_by_name("d_passw").clear()
driver.find_element_by_name("d_passw").send_keys(data[1][0:5])
The above is assuming some fixed lengths of your id and password.
But if you do not want to give predefined values like 3 and 5 as above, try using the length function.