How can I get the inspect element HTML Selenium - python-3.x

I'm trying to get a HTML that is not the page_source, I need the one that is generated when you open the page.
I have seen and tried all the solutions I could find but nothing really works.
I do not need any code since I do not know how to do it. I have tried with page_source but I need the inspect element HTML.
Could someone explain a good example with this?

From Get current HTML of rendered page using Selenium I suggest you using
self.driver.find_element_by_xpath("//html").get_attribute('outerHTML')

Related

Selenium: failing to find element by XPATH Python

I am a little bit new to programming but python really made get into it. I am trying to create a programm that automatically checks for updates in a website. I've successfully implemented the neccessary code to call the page of enrollment but yet there is one element that cannot be located. Since I have to do it for multiple courses and iterate throught them there is no specific id, I've tried to find it by title but also this didn't work.
Is there a way you can locate the button with the title "enroll".
I've tried
driver.find_element_by_xpath("//a\[#title ='enroll']").click()
but this didn't work and I always get
NoSuchElement
error.
The XPATH for the button is simply: //*\[#id="id572"\]
Here is the part of the HTML code:
From the screenshot of HTML code you provided, the element is <button>, not <a>.
Try this xpath expression //button[#title='enroll']
This should do it if it's not in any iframes. Just grab the button whose title is enroll and click. Your css selector was an a tag and it might get id dynamically.
driver.find_element_by_css_selector("button[title ='enroll']").click()

XPath is not working even when directly copied from Chrome inspect

I have been trying to automate my work and we use service-now for our requests. However, for the the life of me I can not get Selenium to run properly on the service-now website. It works on the login page before entering, but no matter what form of locater or x path I use it will not work. The website is dynamic so I am pretty sure xpath is needed.
I have tried directly from Google Chrome inspect as well as other xpath possibilities:
//*[#id="row_sc_request.sc_task.request_65091fb5db8163c4bc8f18df4b961921"]/td[3]/a
xpath=//a[starts-with(text(),'Open record: SCTA')]
xpath=//a[class="linked formlink" and starts-with(#aria-label='Open record: SCTA')]
This is the element copied from chrome
<a class="linked formlink" aria-label="Open record: SCTASK0067185" href="sc\\\_task.do?sys\\\_id=65091fb5db8163c4bc8f18df4b961921\\\&sysparm\\\_record\\\_target=sc\\\_task\\\&sysparm\\\_record\\\_row=1\\\&sysparm\\\_record\\\_rows=1\\\&sysparm\\\_record\\\_list=request%3D9509dbb5db8163c4bc8f18df4b96199f%5EORDERBYDESCnumber">SCTASK0067185</a>
Can someone please review my code? Any help would be appreciated!
I will suggest going with absolute XPath, I guess below note can help
Note:- if you copy XPath from Firefox it will mostly give you absolute XPath whereas chrome on another side will give relative
or the other way is to make an XPath using another stable element in the DOM tree
I hope this workout for yours. if you can share a link or inspect element snapshot showing Full DOM I can help you even better. Thanks :-)
There is a syntax error in the second xpath, this one - xpath=//a[class="linked formlink" and starts-with(aria-label='SCTA')].
In xpath the attributes must be prefixed with the "#" char; and starts-with() takes two arguments, not a boolean. So it must be:
xpath=//a[#class="linked formlink" and starts-with(#aria-label, "SCTA" )]
I don't know for sure will that make it match (it should, based on the sample), but will get you closer.
Thank you for all your answers. It turns out all I needed to do was switch the IFrame.
I am fairly new to this so I had no idea.
In my situatuion, I had to write:
"browser.switch_to.frame(browser.find_element_by_name('gsft_main'))"
where gsft_main is the name of the frame.
After doing this, then I was able to use:
browser.find_element_by_xpath("//a[#class='linked formlink' and starts-with(#aria-label, 'Open record: SCTA' )]")
and it worked out.
A solution might be to switch from an absolute xpath (the one that you find in the browser's developer tools: Copy->Copy XPath) to a relative one. For that you can install an extension like SelectorsHub and look for 'Rel XPath'
In one particular example I discovered that the absolute xpath simply did not work.

How to share html report of pytest-html with out changing the css

I am using python 3.6 and pytest-html to generate HTML reports .
Everything is successfully working but when i share my html report to my manager the css of the entire document is out of placed .can someone tell the reason to why it is happening and the solution for it .
The view of reports when i run:
The view of the reports when i share the document with my manager
include --self-contained-html when you are calling your pytest...such as
pytest your.pyfilename --html=pathandfilename --self-contained-html.
Your result file have inline css in it.
html=report.html --self-contained-html
It seems like you are not sharing whole bunch of items like CSS with html file you are giving. Just place the CSS code inside your HTML rather than giving the path and it will solve your problem.

Capture screenshot of specific element using NodeJS and Browserstack

Does anyone know if there's a way to use NodeJs to drive Browserstack and capture a screenshot of a specific element on the page?
Thanks!
You can do it using PhantomJS.
Screenshot the entire page:
Phantomjs - take screenshot of a web page
Screenshot cropped to the dimensions of a specific element:
Crop screenshot to element in PhantomJS
More PhantomJS examples: https://github.com/ariya/phantomjs/wiki/Examples
The below given links give a sample to capture screenshot of an element using php. You can use it as a reference to write code in Node. If you face any problem, please feel free to ask.
https://github.com/facebook/php-webdriver/wiki/Taking-Full-Screenshot-and-of-an-Element
Exactly what you are looking for:
https://github.com/firstandthird/stackshots

phantomjs // render webpage from give dom

is there any chance to render/process a webpage just from the given DOM?
At the moment we can use page.open but just with a url. In my app i've got the DOM from somewhere else so there is no need to get it twice :)
I now that you can set the page content via page.content = "<html></html>" but i'm missing the callback for loaded images, etc.
Any Ideas out there?
Have a look at casperjs' bbcshot.js code sample, it does quite exactly what you're asking for.

Resources