Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Element not found on a while #1563

Answered by mdmintz
KolyStudio asked this question in Q&A
Discussion options

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)
You must be logged in to vote

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

Comment options

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
You must be logged in to vote
5 replies
Comment options

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.)

Comment options

Do you recommand using CLASS instead of XPATH?

Comment options

CSS Selectors will be much cleaner. The SeleniumBase recorder can generate those instantly from your script.

Comment options

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?

Comment options

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).

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /