How to try clicking on elements in Playwright without try except block? - python

I have this code:
def try_close_error_popup(self):
try:
self.page.locator(
self.CLOSE, has_text='Close').click(timeout=1200)
except TimeoutError:
LOGGER.info('Failed to click close popup button')
I noticed that the try/except blocks make my code to run much slower. Is there a way I can just try finding and element and clicking on it without getting an error?

Try this:
if (page.is_visible(XPATH)):
page.click(XPATH)
Or you can make the code cleaner like this:
try:
page.click(XPATH)
except:
pass

Related

Is there a way to let selenium ignore the timeoutexception Error and continue if it happens?

I am using selenium with python and my chromedriver and chrome.exe is ver90.0
I am having a problem that the script will stop once a timeout error occurs and the problem is that it always happens, sometimes in a few hours, sometimes in a few minutes.
It will show up something like :
selenium.common.exceptions.timeoutexception: message: timeout: timed out receiving message from renderer: -0.014
when this happens, I see the webpage is usually loading and it basically can't load resulting the error occuring.
Is there a way to ignore this error and basically let the script either refresh or just continue loading to the next webpage in the list. An example code will be like this:
while True:
driver.get(thislist[i])
if .......:
i = i + 1
I tried many fixes, but none of them work. I tried with beta chrome.exe and chrome webdriver ver 91.0. I also tried including chrome_options and I also tried some headers that's not in this code, but I always eneded up receiving the timeout error. I asked this question a few times before, but there hasn't bveen a fix that works.
chrome_options.add_argument('--enable-automation')
chrome_options.add_argument('--lang=en')
chrome_options.add_argument("window-size=1920,1080")
chrome_options.headless = True
chrome_options.add_experimental_option ("debuggerAddress", "localhost:8989")
driver = webdriver.Chrome(executable_path='C:/Webdriver/chromedriver', chrome_options=chrome_options)
while True:
try:
driver.get(thislist[i])
except timeoutexception as e:
print(e)
continue
else:
# do what you want here
When there is a timeoutexception, the code will just print it and continue to the next loop. If there is no one, then you can do web driving under "else:".
Thanks for reading.
What about adding a try catch around your operation that raises the error. If there is an error, you simply continue in your loop.
Use Exception since your don't know the kind of exception that will be thrown.
You can always set the timeout for selenium to be longer with the following line: driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

python selenium Attribute error in a while loop while using .click() while trying an except

i=1
while i<=numberCountGlobal:
time.sleep(1)
print("i is: ",i)
self.tempString='//*[#id="grid-view"]/div[{}]/div[4]/button'.format(i)
try:
self.driver.find_element_by_xpath(self.tempString).click()
print("button press sucessfull")
except NoSuchAttributeException:
print("Nothing here.. skipping")
i+=1
error:
self.driver.find_element_by_xpath(self.tempString).click()
AttributeError: 'NoneType' object has no attribute 'click'
This throws an attributeError but when im trying to expect it, it just doesn't work. When I close my browser, then the code error comes up in the console.
It`s trying to click a specific button in a website, there are either a lot of them or a few of them hence I used a while loop to loop through all buttons that are "there"
if for example.. I remove the .click() and just leave it as it is, it still gives no error until i close my browser, then it comes. Also i am not sure why it is giving an attribute error when the error should rather be a NoSuchElementException as this element IS NOT THERE when there is no button! (yes I have tried this as well and it still has the same issue) as well as except AttributeError
So if a button is no there, the xpath will not work as the element is no there, and if it is, it will be in the correct index, so if the 5th button was there, it will be: //*[#id="grid-view"]/div[5]/div[4]/button
I do not know selenium, but if the exception isn't catching it, then that means that you have the wrong type of exception. The reason that it is an "attribute error" rather than the one that you stated is that "self.driver.find_element_by_xpath(self.tempString)" is returning as "None." Based on my work with BeautifulSoup, this could be because it couldn't find an element with the xpath of your self.tempString. The attribute error is derived because python is interpreting it as "None.click()," when there clearly isn't a click attribute for None.
I hope that this helps.
Fix:
add an implicit wait just after the try:
i=1
while i<=numberCountGlobal:
time.sleep(1)
print("i is: ",i)
self.tempString='//*[#id="grid-view"]/div[{}]/div[4]/button'.format(i)
try:
self.driver.implicitly_wait(1)
self.driver.find_element_by_xpath(self.tempString).click()
print("button press sucessfull")
except NoSuchAttributeException:
print("Nothing here.. skipping")
i+=1```

Click on multiple elements using selenium on Python

I would like to create a bot which will retweet all tweet load on an account.
First I try to use this method:
rt = self.driver.find_elements_by_xpath("//div[#data-testid='retweet']")
for i in rt:
time.sleep(1)
i.click()
"""Confirm the retweet"""
time.sleep(1)
self.driver.find_element_by_xpath("//div[#data-testid='retweetConfirm']").click()
But it didn't work, I wasn't able to wait enought before clicking, even with a high time.sleep().
So instead, I try this:
for i in rt:
z=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[#data-testid='retweet']")))
z.click()
#Confirm the retweet
b=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[#data-testid='retweetConfirm']")))
b.click()
Unfortunately this code will only retweet the first tweet. I should modify something in EC.element_to_be_clickable((By.Xpath,"//*[#data-testid='retweet'])) (and in the second EC.element_to_be_clickable) to go to the next tweet for each iteration but I don't know what.
Does anybody know a way to iterate through all my tweets with this method (or another)? I have been thinking about getting the absolute path of all my element in "rt" but I don't know if I can do this using only Selenium. I could also use Twitter API but I want to be able to create a bot on others websites.
Thank you
Ok, I think I have found a solution to my issue.
For each click, I try to catch the exception "ElementClickInterceptedException" when it happens; otherwise I click on my retweet.
So it gaves something like that:
for i in rt:
first_clique=False
while first_clique==False:
try:
i.click()
except ElementClickInterceptedException:
pass
else:
first_clique= True

Stop when error

In python, I am using selenium to scrape a webpage. There is a button that I need to repeatedly click until there is no button anymore. So far, I have code like:
count = 20
while count:
driver.find_elements_by_class_name('buttontext')[0].click()
count-=1
The problem is that I don't actually know how many times I need to do this - count = 20 is incorrect. What I actually need is for it to keep going until the command encounters an error (IndexError: list index out of range), and then stop. How can I do this?
Follow the EAFP approach - make an endless loop and break it once there is no element found:
from selenium.common.exceptions import NoSuchElementException
while True:
try:
button = driver.find_element_by_class_name("buttontext")
button.click()
except NoSuchElementException:
break
You should use the try statement to handle exceptions. It will run your code until it finds an exception. Your code should look something like that:
try:
while True:
click_function()
except Exception: #as Exception being your error
got_an_error() #or just a pass
I would follow the Selenium docs' recommendation and use .findElements().
findElement should not be used to look for non-present elements, use
findElements(By) and assert zero length response instead.
buttons = driver.find_elements_by_class_name("buttontext")
while buttons:
buttons[0].click()
// might need a slight pause here depending on what your click triggers
buttons = driver.find_elements_by_class_name("buttontext")
Do you need this?
while True:
try:
driver.find_elements_by_class_name('buttontext')[0].click()
except IndexError:
break
try will try to run some code, and except can capture the error that you select.
And if you didn't select an error, except will capture all errors. For more info.

Learning to use Assert and Asserttrue in Python for selenium web driver

Im trying to make a python webdriver to load a webpage and then assert true and run a print command if a text or object is there, and if not I want it to just continue running my loop. Im a noob to python and have been learning from Learn python the Hard Way, and reading documentation. Ive spent the last day or so trying to get my code finding text or elements, but it doesnt feed back info...Here is my code so far minus the top part about going to the webpage, I am just stuff on this count loop assert logic.
count = 1000.00
while count < 1000.03:
driver.find_element_by_id("select").clear()
driver.find_element_by_id("select").send_keys(str(count))
time.sleep(1)
driver.find_element_by_id("BP").click()
driver.find_element_by_id("BP").click()
count += 0.01 ## increases count to test
highervalue = driver.find_element_by_link_text("Select Error")
assertTrue(driver.link_contains_match_for("")) ##could also be, ##text_containt_match_for("ex") or driver.assertTrue(element in WEBPAGE)??
print 'Someone is %.7r' %count
else:
print 'I have %.7r' %count
time.sleep(1)
then the loop starts over again. The issue i am having is I want to find "Select Error" on the webpage in some kind of form, link, or text, and then if it is there print me a msg, and if not, to just continue my loop.
Is it better to use assert/asserttrue, or something like
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
or
Some other examples I have searched about that could be used:
self.assertTrue(self.is_element_present(By.ID, "FOO"))
self.assertTrue(self.is_element_present(By.TEXT, "BAR"))
self.assertTrue(self.is_text_present("FOO"))
self.assertTrue(self.driver.is_text_present("FOO"))
Can someone let me know how I would write the part when I find something in the webpage and it gives me feedback if found?
Selenium contains methods known as waits to assist with this specific situation. I would read up on the documentation of explicit waits. Also, there is another Stack Overflow question which may be useful to look at.
In addition, and slightly off topic, it is atypical to use Asserts in the way you have in the code shown. In general, you should not think of using Asserts to check for conditions, but rather a condition that must be true for the test to pass, otherwise the test stops and there is no reason to continue on.
Cheers.
Assuming that your "Select Error" message is actually a link_text, you could try using a 'try/except' statement.
try:
WebDriverWait(driver, 3).until(
expected_conditions.text_to_be_present_in_element((By.LINK_TEXT, "Select Error"), "Select Error")
)
print "Link Found..."
except NoSuchElementException:
pass
This code tries to find the "Select Error" link text for 3 seconds, catches the NoSuchElementException error, and then continues with your code.

Categories

Resources