Change canvas style size inside several browser windows using python - python

I have several browsers open and each has a webpage already open that has some div and canvas elements.
<div class="webgl-content">
<div id="game" class="game" style="padding: 0px; margin: 0px; border: 0px; background: rgb(25, 39, 54);">
<canvas id="#canvas" style="width: 100%; height: 100%; cursor: default;" width="1400" height="800"></canvas>
</div>
</div>
How can I change the height:100% to a different value (say 77%) in all browser windows using python?
I am thinking something that can identify the open browsers, then find the canvas element and change the style height percentage to a new value.
I've been playing around with pywinauto library but not much progress so far: I get errors indicating that Chrome application was not found.

Related

Trying to scrape image and I get empty output

I'm trying to scrape Twitter account image, I tried multiple ways and the output keeps give me empty list!
My Code:
import requests
from bs4 import BeautifulSoup
url = requests.get('https://twitter.com/jack/photo')
soup = BeautifulSoup(url.text, 'lxml')
image = soup.find_all('img')
print(image)
Output:
[]
That's a part of my project .. I tried lxml and find by class, but I still get nothing, maybe I'm missing something there but I don't know what it is.
If anyone can help me with it, I will be so appreciated.
Thanks in advance
I can see some React being used in the page. If you open the page and inspect the elements, you will see that as soon as you click on the photo to enlarge it, a new div appears as if from thin air. Which implies that that get created by react.
In order to address this you will need to use Selenium to open the page in a virtual browser, let the JavaScript do its magic and then look for the img tag.
You're trying to scrape the path for JavaScript twitter. If you examine the response of your page you will see the following snippit.
<form action="https://mobile.twitter.com/i/nojs_router?path=%2Fjack%2Fphoto" method="POST" style="background-color: #fff; position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 9999;">
<div style="font-size: 18px; font-family: Helvetica,sans-serif; line-height: 24px; margin: 10%; width: 80%;">
<p>We've detected that JavaScript is disabled in your browser. Would you like to proceed to legacy Twitter?</p>
<p style="margin: 20px 0;">
<button type="submit" style="background-color: #1da1f2; border-radius: 100px; border: none; box-shadow: none; color: #fff; cursor: pointer; font-size: 14px; font-weight: bold; line-height: 20px; padding: 6px 16px;">Yes</button>
</p>
</div>
</form>
I would recommend disabling javascript in your browser and then figuring out how to view the photos like that. Then you could mimic those requests using requests.
What worked for me was sendind a request to the path:
https://mobile.twitter.com/jack
Then using the css selector: class = "avatar". There should be one child, an image tag, grab the src of that image tag and that should be the link to your photo.
As requested, here is the python code I used:
import requests
from bs4 import BeautifulSoup
response = requests.get('https://mobile.twitter.com/jack')
soup = BeautifulSoup(response.text, 'lxml')
avatars = soup.findAll("td", {"class": "avatar"})
print(avatars[0].findAll('img')[0].get('src'))
Note: Twitter changes their layout frequently, so this may not work for long.

Jupyter Notebook scrollbar doesn't disappear? [duplicate]

I am developing an application with a few components that are scrollable horizontally. I've come across some unwanted behaviour where the horizontal scrollbar does not disappear leaving an ugly long white scrollbar.
My 'show scroll bars' setting on MacOS is set to 'Automatically based on mouse or trackpad'. My chrome is Version 72.0.3626.121 (Official Build) (64-bit).
The issue can be reproduced in the snippet below.
#horizontal {
width: 100%;
height: 150px;
overflow-x: scroll;
overflow-y: hidden;
flex-direction: row;
border: 2px solid purple;
display: flex;
}
#vertical {
width: 300px;
height: 300px;
overflow-x: hidden;
overflow-y: scroll;
flex-direction: column;
border: 2px solid purple;
display: flex;
}
.horizontal-item {
min-width: 100px;
width: 100px;
min-height: 100px;
height: 100px;
margin-right: 24px;
margin-bottom: 24px;
background-color: pink;
display: flex;
}
.vertical-item {
min-width: 100px;
width: 100px;
min-height: 100px;
height: 100px;
margin-right: 24px;
margin-bottom: 24px;
background-color: red;
display: flex;
}
<div id="horizontal">
<div class="horizontal-item">1</div>
<div class="horizontal-item">2</div>
<div class="horizontal-item">3</div>
<div class="horizontal-item">4</div>
<div class="horizontal-item">5</div>
<div class="horizontal-item">6</div>
<div class="horizontal-item">7</div>
<div class="horizontal-item">8</div>
<div class="horizontal-item">9</div>
<div class="horizontal-item">10</div>
<div class="horizontal-item">11</div>
<div class="horizontal-item">12</div>
<div class="horizontal-item">13</div>
<div class="horizontal-item">14</div>
<div class="horizontal-item">15</div>
<div class="horizontal-item">16</div>
<div class="horizontal-item">17</div>
<div class="horizontal-item">18</div>
<div class="horizontal-item">19</div>
<div class="horizontal-item">20</div>
</div>
<div id="vertical">
<div class="vertical-item">1</div>
<div class="vertical-item">2</div>
<div class="vertical-item">3</div>
<div class="vertical-item">4</div>
<div class="vertical-item">5</div>
<div class="vertical-item">6</div>
<div class="vertical-item">7</div>
<div class="vertical-item">8</div>
<div class="vertical-item">9</div>
<div class="vertical-item">10</div>
<div class="vertical-item">11</div>
<div class="vertical-item">12</div>
<div class="vertical-item">13</div>
<div class="vertical-item">14</div>
<div class="vertical-item">15</div>
<div class="vertical-item">16</div>
<div class="vertical-item">17</div>
<div class="vertical-item">18</div>
<div class="vertical-item">19</div>
<div class="vertical-item">20</div>
</div>
The problem occurs when you hover over the bottom of the horizontal scrollable area (so where the scrollbar will appear, the purple bottom of the container with pink squares). The scrollbar will appear and never leave again. The same does not happen with the vertical scrollable area, where the scrollbar also appears but does disappear. If you scroll the scrollbar before hovering over the bottom then afterwards said problem won't occur if you hover over where the scrollbar would appear.
In the image below I hovered over the bottom of the horizontal scrollable area and it shows the scrollbar is there (and it does not leave afterwards!).
This problem infact also occurs when I hover over the horizontal scrollbar from a stackoverflow code block, making text hardly readable.
Long line of text Long line of text Long line of text Long line of text Long line of text Long line of text Long line of text Long line of text
It will look like this and the scrollbar wont disappear anymore much like in my own case:
I'm assuming this is a bug in Chrome with MacOS but I was hoping there may be some CSS tricks I can do to solve this problem.
This is Chrome issue: https://bugs.chromium.org/p/chromium/issues/detail?id=914844#c36
Many people are adding white space of scrollbar size (25px) to prevent scrollbar from obscuring content.
It is workaround and can be considered only as a temporary solution though.
From the ticket, they give a workaround until the issue is fixed:
Go into your System Preferences > General
Select Always:
We have been having this issue in our Macs with same OS version, same chrome versions. The final conclusions we got are the following:
the ones using the Apple Original mouse and trackpad are able to see all normal.
When we connect to the same computer one standard USB mouse, after reload the web we suddenly got the annoying scrollbars.
It was tested and same happened in 3 different MacBook Pro.
I upload a video here what happens when I plug out and in: https://youtu.be/AGTF2Ltuxnk
EDIT
Our custom solution was prevent default scroll bars and set up our own scroll bars that will only be displayed when neededneeded.
::-webkit-scrollbar-track {
display: none;
border-color: transparent;
background-color:transparent;
}
::-webkit-scrollbar * {
background:transparent;
}
::-webkit-scrollbar {
width:rem(7);
min-width:rem(7);
height:rem(7);
min-height:rem(7);
}
::-webkit-scrollbar-corner {
background-color:transparent;
}
::-webkit-scrollbar-thumb {
border-radius:rem(10);
background-color:#666;
-webkit-box-shadow: inset 0 0 0 ;
}

Not able to click on drop down option in a ajax webpage using selenium python

I am able to click on the drop down heading but not able to click on the options. Also I am not able to identify the 'id' or xpath for options which is visible after clicking the dropdown option.
Find the HTML below:
<div id="object260310" style="position: absolute; overflow: hidden; background: transparent; z-index: 50; left: 154px; top: 5px; width: 74px; height: 20px;">
<div id="object351" style="position: absolute; z-index: 11; left: 0px; top: 0px; width: 74px; height: 20px; background-color: rgb(255, 255, 255);">
<div style="position: absolute; width: 42px;">
<div role="menu" aria-label="1" class="font89" style="padding-left: 0px; cursor: pointer; position: absolute; left: 0px; color: rgb(126, 126, 126); width: 43px; height: 20px; line-height: 20px; background: transparent;" onclick="plw.menu.click(this,351,0,"1",true);" onmouseenter="plw.menu.over(event,this,351,0,"1");this.style.color="rgb(174,174,174)";this.style.backgroundColor="rgb(255,255,255)"" onmouseleave="plw.menu.out(351);this.style.background="transparent";this.style.color="rgb(126,126,126)";">
<div style="position:absolute;left:0px;top:2px" class="image347 "></div>
<span style="position:relative;left:21px;top:0px">New</span>
</div>
</div>
</div>
</div>
Below is my selenium code:
new_create = WebDriverWait(driver, 40).until( EC.presence_of_all_elements_located((By.XPATH, '/html/body/div[2]/div[2]/div[5]/div/div/div/div')))
driver.find_element_by_id("object260310").click()
#its working fine till here
driver.find_element_by_xpath(".//*[contains(#onclick, '231')]").click()
#This line doesnt seems to work.
And here is a screenshot of the site.
Clicking drop down options have been iffy for me before. This is a thing I do when there aren't any other solutions:
Click the drop down then you can send keys depending on the letter of the option you need. so if an option is "action" then you press "a".
This highly depends on what you have in the drop down though.
EDIT: I would highly look at all the related questions there... They have some stuff you might be able to use. As I said this is only if those don't work!
You can try to click on the drop-down first(New in your case)
If the dropdown is not select , then use the below code select the value.
driver.find_element_by_xpath("//div[text(),'Product Variation']").click();
driver.find_element_by_xpath("//div[contains(text(), 'Product Variation']").click();
Think the xpath should be written that way. Will you send me the URL and I'll make sure I can click before I make another edit on this post?
Thank you all for your help. I was able to find the xpath using chrome XPATH Helper extension.Extension gave below xpath :
/html/body/div[#id='m235e0-SUB-1']/table[#class='oldmenu']/tbody/tr[#id='235-0-SUB-1-1']/td[#class='oldmenu']
Using this I was able to identify the id for sub menu i.e. '235-0-SUB-1-1'
So I modified the code accordingly as below :
driver.find_element_by_xpath('//*[#id="235-0-SUB-1-1"]').click()

Python Webdriver how to handle a browser pop up. It is not an alert generated by JavaScript

I have come accross a pop up while my Python Webdriver script was executing.
Scenario: User can click Add project button from the Admin page and enter a project name etc click Save.
If the project already exists a pop up is shown to say project with this name already exists.
I want to close this pop up. I have inspected the HTML and there is no on click JavaScript function to call the alert. It is not a JavaScript alert.
It is a pop up generated by GWT in the browser.
The dev uses GWT to develop the UI.
The HTML is:
<div class="gwt-PopupPanel" style="visibility: visible; overflow: visible; position: absolute; left: 687px; top: 170px;">
<div class="popupContent">
<div class="GAT4PNUJO" style="position: relative;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div class="GAT4PNUPO GAT4PNUNO">
<div class="gwt-Label GAT4PNUOO">Add Project</div>
</div>
<div class="GAT4PNUMO">
<div class="gwt-HTML GAT4PNUIO GAT4PNUKO">An engine message has been returned. Original message: There is already a scv project with name 'test001'</div>
<div class="GAT4PNUHO GAT4PNULO">
</div>
</div>
</div>
</div>
Is there a way I can check if the pop up is shown and then click it in Python Webdriver?
I have read in another post someone mentioned to use Robot. I think that is Java only.
I have tried
alert = self.driver.switch_to_alert()
alert.accept()
just to check and prove it is not a JavaScript alert. The error shown is
NoAlertPresentException: Message: No alert is active
I have managed to solve this now. I used the following code:
self.driver.find_element_by_xpath("//*[text()='OK']").click()
The Ok button is clicked on in IE and the pop up closes.
I had the same when trying to leave a page, a pop-up came and asked if I was sure I want to leave the page. I solved it with FF settings:
webdriver.DesiredCapabilities.FIREFOX["unexpectedAlertBehaviour"] = "accept"
I then catch the exception and do what I want, which is leave the site. I hope this works for you too. You have to catch the exception, because it still causes the exception to come up and stop your code.

How to select font size bgcolor using selenium

this is my html code and i need to select font size, bgcolor which is there in nonscript.
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
<!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
<noscript>
<div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
Your web browser must have JavaScript enabled
in order for this application to display correctly.
</div>
</noscript>
can any budy help with this.
I am using python as my scriptin language.
you just want to grab the text? you can look at something like beautiful soup (which i'm not familiar with), or use a simple regex
import re
m = re.compile(r'background-color: (\w+);',re.I)
result = m.search(text)
if result:
bgc = result.group(1)

Categories

Resources