-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Element not found on a while #1563
-
Hello, i'm trying to solve a problem.
When I make a loop, my script works perfectly. However, at some point, I don't know why, it can't find one of the elements. Maybe because one of the actions before the one it is going to do has not been done, and so it stops the script.
I thought of putting sleeps, or if else, or try catch, with continue;
But is there any other solution with Seleniumbase?
Thanks a lot!
while(newgroups<100):
self.sleep(0.2)
self.click("/html/body/main/div[1]/div[1]/nav/div[1]/div/div/div[2]/div")
self.click("/html/body/main/div[1]/div[1]/div[1]/div[3]")
newgroups = len(self.find_elements('div.RbA83'))
self.click_nth_visible_element("div.Ewflr", 1)
self.click_nth_visible_element("div.Ewflr", 2)
self.click("/html/body/main/div[1]/div[2]/div/div[2]/div/form/div[4]/button[1]")
self.sleep(0.2)
self.send_keys("/html/body/main/div[1]/div[2]/div/div/div/div[2]/div[2]/div/div/div/div", "a")
self.click("/html/body/main/div[1]/div[2]/div/div/div/div[2]/div[2]/div/div/div/button")
self.click("/html/body/main/div[1]/div[2]/div/div/div/div[1]/div[1]/div/div[1]")
self.sleep(0.2)
self.click("/html/body/main/div[1]/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div[2]/div[2]/button")
self.click_nth_visible_element("div.Zp5By", 3)
self.click("/html/body/main/div[1]/div[2]/div/div/div/div[1]/div[1]/div/div[2]/div/div[4]/div/button")
newgroups = newgroups + 1
print('Groupes crées:', newgroups)
Beta Was this translation helpful? Give feedback.
All reactions
You might want to use self.type()
instead of self.send_keys()
, because self.send_keys()
won't clear out the text field first if there's text already in it. Also, it's difficult to see what your script is trying to do because you're using long selectors that might not be very stable.
You can probably optimize most of your selectors if you run the following on your script:
pytest --rec-print
Replies: 1 comment 5 replies
-
You might want to use self.type()
instead of self.send_keys()
, because self.send_keys()
won't clear out the text field first if there's text already in it. Also, it's difficult to see what your script is trying to do because you're using long selectors that might not be very stable.
You can probably optimize most of your selectors if you run the following on your script:
pytest --rec-print
Beta Was this translation helpful? Give feedback.
All reactions
-
After running pytest --rec-print
, updated scripts will appear in ./recordings/
up to the point where the script failed. (Or will show everything if the script passed.)
Beta Was this translation helpful? Give feedback.
All reactions
-
Do you recommand using CLASS instead of XPATH?
Beta Was this translation helpful? Give feedback.
All reactions
-
CSS Selectors will be much cleaner. The SeleniumBase recorder can generate those instantly from your script.
Beta Was this translation helpful? Give feedback.
All reactions
-
I have this result,
self.click('input[placeholder="Rechercher"]')
self.click('button[title="Nouveau Chat"] svg path')
self.click("main#root div:nth-of-type(2) div:nth-of-type(2) form ul li:nth-of-type(2) div:nth-of-type(2) span span span:nth-of-type(3)")
self.click("main#root div:nth-of-type(2) div:nth-of-type(2) form div:nth-of-type(4) button div")
Is that a good result?
Beta Was this translation helpful? Give feedback.
All reactions
-
The first two lines are definitely cleaner. The algorithm won't necessarily find the perfect selector for each one. Looks like several lines are missing from your original script (or you only copied a few over).
Beta Was this translation helpful? Give feedback.