Gives data only through debugging selenium - python

I have a problem, I have a code
browser1.get("https://www.artstation.com/artwork/LeAN1P")
element = browser1.find_elements(by=By.CSS_SELECTOR, value='div.d-flex:nth-child(2) > span:nth-child(2)')
for el in element:
print(el.text)
I need to get the Views parameter, but everything ends with the successful execution of the code, despite the fact that if you run it through debug, then everything works
Debug console
Run console
if you integrate the code into another, then an error pops up, tell me how you can get the Views text data from this page
launch in another finished project
no ideas please help

Your elements are simply not loaded yet when script in run mode, as it's faster than debug. Before finding element wait for it to be located/visible etc
https://selenium-python.readthedocs.io/waits.html

Related

Selenium Python not finding the element

I'm trying to click on some element but it's not working:
driver.find_element_by_xpath("//span[text()='ENG']")
When I add:
driver.maximize_window()
before click action, it works, other codes are not working again.
I had similar problem
When I was looking for an element, it was not yet available in the code.
Fixed by adding
driver.implicitly_wait(30) ## 30 is the time he will wait
before searching for the element.
This line makes the code wait until the entire page is loaded before looking for an element.
I'm trying to click on some element via driver.find_element_by_xpath("//span[text()='ENG']"), but it's not working,
You need to post logs or be more specific. Are you getting an error on the call to find_element_by_xpath(), or when you call click as you mention below?
when I add driver.maximize_window() before click action, it works,
other codes are not working again, please advise which can be the
reason
The relevant code needs to be provided. I believe you're saying that when you call maximize_window() before you locate the element, it works, but if you don't call maximize window, it fails. This could be for a variety of reasons, but it sounds Javascript related. A similar question like this this could help. If maximize window doesn't actually help the issue, I would look into implicit waits or WebDriverWaits.

VS Code clear previous output before each run

Extensions installed: Python, Code Runner
The problem is I was trying to enable the "clear previous output before each run setting"
But when I try running a simple code the output (in terminal) becomes glitchy.
The code I am running is:
print('hello')
a=input()
print(a)
Here is the first time I run the code in completely Blank terminal
The first attempt is pretty normal.
Here is the what happens when I run the code again
There is a lot of blank space in the terminal which becomes worse the third time I run the code
This blank space worsens till the 5th attempt and here is what happens on the sixth attempt.
Basically the blank space vanishes and all the previous attempts can be seen in the terminal. But that means the initial setting I intended did not apply and terminal does not get cleared before each run.
Can someone tell how to automatically clear terminal before each run.
"code-runner.clearPreviousOutput": true works well in OUTPUT panel, but does not work in the terminal.
Someone else has submitted a feature request for it. You can refer to this page and upvote it.

How can I avoid inconsistent behavior with Selenium StaleElementReferenceException (Python)?

I'm working on a bit of automation that basically opens YouTube, plays a particular video, then opens the "Stats for Nerds" dialog and grabs the info from it. This was working yesterday. Today, I added lines to set the video to 1080p, then go into full screen, and now it fails to work.
The following line works:
driver.find_element_by_id("movie_player").click()
While this set fails, throwing a StaleElementReferenceException:
element = driver.find_element_by_id("movie_player")
actions.move_to_element(element)
actions.context_click(element)
actions.perform()
Nothing I have done can avoid this. I've tried putting it in a try block, with and without some waits thrown in. Somehow the element is going stale in the very short time between finding and setting the element, and performing the context_click action.
Is there any way of getting around this?
Edit: I'll keep checking back in to see if someone knows a better way to do this, but for now, in the interest of expediency, I've just kludged it by using pymouse to actually take control of the mouse and right click.
I suspect the DOM changes if you mouse over the button. To workaround it, "refind" the element:
element = driver.find_element_by_id("movie_player")
actions.move_to_element(element).perform()
element = driver.find_element_by_id("movie_player")
actions.context_click(element).perform()

iPython Notebook in Firefox - Warning: unresponsive script

I had a long script running on iPython notebook in Firefox for a long time. I came back and it seemed to have hung up, so I saved it and closed it.
When I re-open the script, I get a dialog box pop up with the following error:
Warning: unresponsive script
A script on this page may be busy, or it stack overflow may have
stopped responding. You can stop the script now, open the script in
the debugger, or let the script continue.
The options are 'continue', 'stop script' and 'debug script'. Clicking on any of these leads to the same result, the dialog box disappears and the iPython notebook is unresponsive (for example, ctrl+enter creates a line-break in the current cell rather than executing it).
Worst of all however, the cells at the bottom of the script seem to have been cut off. They contained some valuable code which seems to have gone, this is quite a bad outcome!
I've tried rolling back but the last roll-back point also shows the bottom cut off. Any support here much appreciated!
I have now worked this out and leave it here in the hope it will help others.
The cause of the problem seemed to be an excessively long output from one of the cells - everything below this line had been cut off in the iPython browser, but I discovered it still exists in the .ipynb file and all I had to do was remove some of the output lines, and when I re-opened the file it ran without problems AND my code that had been cut off was available once again.
The notebooks store everything in JSON format. In my case, I needed to remove output from one of the cells, which I did like this:
Browse to your iPython Notebooks directory (NOT where ipython.exe resides) - for me they were in C:\Users\myname\Documents\IPython Notebooks
Right-click on the offending notebook.ipynb file, and edit in a text editor - my choice is Notepad++
Scroll down to the cell which has generated lots of output lines. Each of these lines is inside the cell's outputs property, with "output_type": "stream"
Remove an arbitrary number of these output entries, but be sure to remove anything outside the output property itself, and be sure to remove from the back of a tailing comma to the front of the following comma so that the resulting JSON is well-formed
A typical line of output looks like this, deleting several hundred of them made my script run again in the browser:
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Added 150000 records so far"
]
},
Maybe it's Firefox's fault.
Letting the script run longer
If you find that pressing the Continue button brings up the same dialog again, letting the script run longer won't help you; it will just make Firefox hang for longer. However, if you can use Firefox normally after pressing Continue, then the script may just needs extra time to complete.
To tell Firefox to let the script run longer:
In the Location bar, type about:config and press Enter. The about:config "This might void your warranty!" warning page may appear. Click I'll be careful, I promise! to continue to the about:config page.
In the about:config page, search for the preference dom.max_script_run_time, and double-click on it.
In the Enter integer value prompt, type 20.
Press OK.
With scripts now allowed to run for longer times, you may no longer receive the prompt.
This is from https://support.mozilla.org/en-US/kb/warning-unresponsive-script
Hope it could help.

Python Selenium WebDriverWait and Click inconsistently giving StaleElementReferenceException()

Ok, here goes my attempt to explain this problem that I haven't even figured out for myself yet. I'm using Selenium with the python-bindings and seem to have an issue with a page element randomly not being found when when using WebDriverWait followed by a click() event. Here is my code:
yearOption = WebDriverWait(self.br, 40).until(lambda d: d.find_element_by_xpath("//select[#name='ctl00$holdSection$rptCommissionYears']/option[#value='%s']" % year), self.br)
print yearOption.text
yearOption.click()
This command set is in a for loop and will randomly fail on the .click() event producing the error: StaleElementReferenceException() after the yearOption.text is printed. This is completely odd to me since the WebDriverWait line obviously found the element, and I haven't reloaded or changed the browser state before clicking the element...
Any ideas why I would be getting this error? Remember, it doesn't happen consistently, infact--sometimes, my entire script will execute successfully with no errors.
I have run into a similar issue before, and I am 99% sure that your problem is the same.
If you check your loop is:
Look for this element
Click
Look for same element
CLick
Normaly after a Click, some page reload or changes occur. This may affect to the element that you are searching for. And if you don't take care, you may end up looking for the element before is reloaded, and when you click it, the element ID already changed, therefore giving you a Stale exception.
Lets go one by one:
Page loads and you element has ID=1
You find it.
You click it and reload/changes start to happen
You enter another loop and find the element. Notice that this even
can happen really fast as there is no wait after click, and thus
find can exit giving you ID=1 element again. You try to click the
element ID=1, but since it was reloaded, it does not exist anymore.
You can fix this in different ways:
If speed is not an issue, you can add an explicit wait after click
of a few seconds, giving enough time to javascript to finish.
You can save the ID of the element, and every time you look for it,
you check that is different, and if it is not, you wait and retry.
Giving this, if that is not your problem, you can always share more of your code and your testing target and I would be happy to help.

Categories