Issue
I am trying to automate taking screenshots from online Power BI reports using Python. I decided to use the By.XPATH method for finding the correct element, but I'm having some difficulties. Here's how the HTML for this part looks like;
<visual-container class="visual-container-component" _nghost-tue-c97=""><transform _ngcontent-tue-c97="" visualcontainerfielddraghover="" data-automation-type="visualContainer" data-testid="visual-container" _nghost-tue-c95="" drag-resize-disabled="true" preserve-aspect-ratio="true" class="bringToFront" style="height: 313px; width: 722px; transform: translate(14px, 59px); z-index: 6000;">
<div _ngcontent-tue-c97="" ng-non-bindable="" skip-children-focus="" extended-shortcut-scope="" toolbar-anchor="" class="visualContainer unselectable readMode hideBorder visualHeaderAbove droppableElement ui-droppable" role="group" aria-roledescription="Lineplot" tab-order="2000" tabindex="0" aria-hidden="false" touch-action="auto" aria-label="Employment rate">
<p _ngcontent-tue-c97="" role="tooltip" aria-hidden="true" localize="VisualContainer_VisualsEnterHint_OnView" class="visualsEnterHint" id="visualsEnterHint-5d5d9e204e6a18618e70">Hit Enter to inspect</p>
<!---->
<!---->
<p _ngcontent-tue-c97="" role="tooltip" aria-hidden="true" class="visualsEnterHint" id="visualsLabel-5d5d9e204e6a18618e70">"Unemployment in area"</p>
<div _ngcontent-tue-c97="" class="visualContainerBorder themableBorderColor">
Now, the last line seems to be the correct line for me to call to capture a screenshot of the correct element.
I have tried using a full XPATH for this and found out that it is working, so in this case
/html/body/div[1]/report-embed/div/div[1]/div/div/div/div/exploration-container/div/div/docking-container/div/div/div/div/exploration-host/div/div/exploration/div/explore-canvas/div/div[2]/div/div[2]/div[2]/visual-container-repeat/visual-container[14]/transform/div/div[1]
However, the relative XPATH I made does not work when running the script.
time.sleep(5.0)
image_binary = drvr.find_element(By.XPATH, ".//div[@aria-roledescription='Lineplot']/div[1]").screenshot_as_png
im = Image.open(io.BytesIO(image_binary))
im.save(filename)
This gives me a NoSuchElementException
, even though I am able to find the correct line from Chrome's dev tools when pasting my XPATH there. This also works when using the full XPATH for this line.
Solution
The issue was in language. When inspecting the HTML for the page, certain values were given in my native language, however when the web driver opened the page, these values were given in English instead. Changing this solved the issue.
Answered By - ceojii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.