Why can't I upload a picture with selenium? PROBLEM FIXED - python

my previous code looks like this, but I cannot upload an image. The script breaks off at this point, I already have several options and try unfortunately without success
I work with VS Code on Ubuntu
myvariable1 = browser.find_element_by_xpath("/html/body/div[1]/div[5]/div/div[2]/div/form/ul[9]/div/li[2]/div/div[1]/div/div[2]")
myvariable1.send_keys("/home/username/Schreibtisch/myfolder/1.png")
The html code looks like this ----
<div class="fileuploader fileuploader-theme-dragdrop">
<input
class="gallery_media"
type="file"
name="files[]"
tabindex="-1"
accept="image/*"
multiple="multiple"
style="position: absolute; z-index: -9999; height: 0px; width: 0px; padding: 0px; margin: 0px; line-height: 0; outline: 0px; border: 0px; opacity: 0;"
/>
<div class="fileuploader-input-inner">
<div class="fileuploader-main-icon"></div>
<h3 class="fileuploader-input-caption onDesktop"><span class="onDesktop">Bilder hierher ziehen und ablegen</span></h3>
<p class="onDesktop">oder</p>
<div class="fileuploader-input-button"><span>Datei auswählen</span></div>
</div>
</div>
<div class="fileuploader-items"><ul class="fileuploader-items-list"></ul></div>

myvariable1 = browser.find_element_by_xpath("/html/body/div[1]/div[5]/div/div[2]/div/form/ul[9]/div/li[2]/div/div[1]/div/div[2]/input")
myvariable1.send_keys("C:/home/username/Schreibtisch/myfolder/1.png")
Go down into input otherwise you'll get a Noninteractable exception and then send the absolute file path to your file.

Related

How to get the iframe elements by TAG_NAME within a WebElement with style attribute set as display: none; using Selenium python

I am using an undetectable Chrome Selenium webdriver in python to try and capture all iframe elements within the div tag with the px-captcha id. The html section I am trying to get from the whole page source looks as such:
<div id="px-captcha" role="region" aria-label="Human challenge" style="display: block; min-width: 310px;">
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: block; width: 100%; height: 100px; border: 0; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -ms-user-select: none; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<p id="ejjpkyuhrAUbSZp" role="alert" style="color: #e50000; display: inline-block; margin: 0; vertical-align: middle;"></p>
</div>
I manage to get the px-captcha id with:
element = driver.find_element(By.ID,'px-captcha')
This gives me a WebElement from which I want to get all the iframe elements. I tried it with this but it fails:
iframes = element.find_element(By.TAG_NAME,'iframe')
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"iframe"}
EDIT
The desired output is a list of the iframe WebElements similar to the type given by element = driver.find_element(By.ID,'px-captcha')
Almost all the descendant iframes are having:
style="display: none;
sans one.
To get the count of all the descendant iframes you need to induce WebDriverWait for the presence_of_all_elements_located() and you can use the following Locator Strategy:
element = driver.find_element(By.ID,'px-captcha')
print(len(WebDriverWait(element, 20).until(EC.presence_of_all_elements_located((By.TAG_NAME, "iframe")))))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try that (Python):
try:
iframes = driver.find_elements(By.TAG_NAME,'iframe')
for frames in iframes:
print(frames.get_attribute('....'))
except Exception as e:
raise e
(Replace '....' by what you want)
Remeber "find_elements" is array, so you need a for or something like that.
To interact with everything inside the frame you need:
driver.switch_to.frame(iframe)
You can return to main frame with:
driver.switch_to.default_content()

Selenium click() - selects the button but doesn't click

I'm using Selenium Python to do something like a Robotic Process Automation. However, I am facing problems clicking in a button...
When I click the Search button manually nothing happens, but through Selenium the alert appears:
The code I'm using is:
try:
driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002 > div:nth-child(1)').click()
except Exception as e:
print(e)
The html section of the button is:
<fieldset class="PageBodyHorizontal" arbwidth="0" arbw="0,0,0,0" aropacity="1.0" arcolor="c0c0c0" arbcolor="#c0c0c0" style="width: 970px;">
<legend class="hidden acc">Form Control Right Panel</legend>
<div class="PageBody pbChrome" style="border-radius: 0px 0px 0px 0px ;-moz-border-radius: 0px 0px 0px 0px ;-webkit-border-radius: 0px 0px 0px 0px ;background: -moz-linear-gradient(top, rgba(192,192,192,1.0), rgba(192,192,192,1.0));background: -webkit-gradient(linear, center center, center center, from(rgba(192,192,192,1.0)),to(rgba(192,192,192,1.0)));background: linear-gradient(rgba(192,192,192,1.0), rgba(192,192,192,1.0));background-color:#c0c0c0;">
<a href="javascript:" id="WIN_3_1002" arid="1002" artype="Control" ardbn="Query" artcolor="null" class="btn btn3d arfid1002 ardbnQuery" style="top: 5px; left: 10px; width: 50px; height: 21px; visibility: inherit; z-index: 997;" arwindowid="3">
<div class="btntextdiv" style="top:0px; left:0px; width:50px; height:21px;">
<div class="f1" style=";width:50px">Search</div>
</div>
</a>
</div>
</fieldset>
It's strange because I have a similar code that works on other pages, for the same button.
The html of a similar button:
<fieldset class="PageBodyHorizontal" arbwidth="0" arbw="0,0,0,0" aropacity="1.0" arcolor="c0c0c0" arbcolor="#c0c0c0" style="width: 1654px;">
<legend class="hidden acc">Panel2</legend>
<div class="PageBody pbChrome" style="border-radius: 0px 0px 0px 0px ;-moz-border-radius: 0px 0px 0px 0px ;-webkit-border-radius: 0px 0px 0px 0px ;background: -moz-linear-gradient(top, rgba(192,192,192,1.0), rgba(192,192,192,1.0));background: -webkit-gradient(linear, center center, center center, from(rgba(192,192,192,1.0)),to(rgba(192,192,192,1.0)));background: linear-gradient(rgba(192,192,192,1.0), rgba(192,192,192,1.0));background-color:#c0c0c0;">
<a href="javascript:" id="WIN_2_1000005683" arid="1000005683" artype="Control" ardbn="z3Btn Function Print Preview" artcolor="#" class="btn btn3d btnd arfid1000005683 ardbnz3BtnFunctionPrintPreview" style="top:5px; left:149px; width:50px; height:21px;color:#;z-index:999;" arwindowid="2">
<div class="btntextdiv" style="top:0px; left:0px; width:50px; height:21px;">
<div class="f1" style=";width:50px">Print</div>
</div>
</a>
<a href="javascript:" id="WIN_2_1002" arid="1002" artype="Control" ardbn="Query" artcolor="null" class="btn btn3d arfid1002 ardbnQuery" style="top: 5px; left: 10px; width: 50px; height: 21px; visibility: inherit; z-index: 997;" arwindowid="2">
<div class="btntextdiv" style="top:0px; left:0px; width:50px; height:21px;">
<div class="f1" style=";width:50px">Search</div>
</div>
</a>
<a href="javascript:" id="WIN_2_303060100" arid="303060100" artype="Control" ardbn="z3Btn_NextStage" artcolor="null" class="btn btn3d arfid303060100 ardbnz3Btn_NextStage" style="top:5px; left:64px; width:82px; height:21px;z-index:998;" arwindowid="2">
<div class="btntextdiv" style="top:0px; left:0px; width:82px; height:21px;">
<div class="f7" style=";width:82px">Next Stage</div>
</div>
</a>
</div>
</fieldset>
If you have advices on the quality of my code and how to fix this problem, I would be grateful.
Click on the a tag instead of div as the href to trigger some javascript code is on the a tag and not div.
driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002').click()
Or try with action class
elem = driver.find_element(By.CSS_SELECTOR, '#WIN_3_1002 > div:nth-child(1)')
Webdriver.ActionChain(driver).move_to_element(elem).click()
try javascript executor:
driver.execute_script("arguments[0].click()",elem)
Generally <div> tags are not interactable unless contenteditable="true" is set.
Some more details about the usecase would have helped us to analyze the observations in a canonical way. However to click on an element ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Using CSS_SELECTOR I:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#WIN_3_1002 > div.btntextdiv > div.f1"))).click()
Using CSS_SELECTOR II:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn3d.ardbnQuery[artype='Control'][ardbn='Query']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Using HTML forms input for python code and getting output back in HTML webpage

I´ve been looking for answers and just lost the sight in all the different ones here.
I am bulding a tool, where you can enter a funding ID (included in papers) and a python code collects different papers from different websites and gives them a score in relevance.
My problem: I built a website with html/css and now I want to use the entered funding ID in one of the forms to pass it on to my python program. I know that i can use action in the form in my html to connect my html file with a different file. I read a lot of things about CGI and servers and Apache, etc. others talked about flask. I just want to find a simple way to exchange information from my html file and my python code and how can I display the information I got from my code in an HTML website?
Thank you!
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: "Verdana", sans-serif;
}
.header {
padding: 60px;
text-align: center;
background: #0C0040;
color: white;
font-size: 30px;
}
.text_header {
margin-left: 160px;
letter-spacing: 6px;
}
.sub_header {
color: #00BFFF;
letter-spacing: 4px;
}
.sidenav {
height: 100%;
width: 160px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #474e5d;
overflow-x: hidden;
padding-top: 20px;
}
.sidenav a {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 25px;
color: #00BFFF;
display: block;
}
.sidenav a:hover {
color: #f1f1f1;
}
.main {
margin-left: 160px; /* Same as the width of the sidenav */
font-size: 28px; /* Increased text to enable scrolling */
padding: 0px 10px;
text-align: center;
}
.title{
color: #0C0040
text-align: center;
}
#media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
</style>
</head>
<body>
<div class="header">
<div class="text_header">
<h1>Looking for more?</h1>
<div class="sub_header">
<p>find equally relevant papers from the same funder</p>
</div>
</div>
</div>
<!--- so far only the About Us page is linked --->
<div class="sidenav">
About Us
Services
Contact
</div>
<div class="main">
<div class="title"> <h2>Insert your funding ID here:</h2></div>
<!--- this is the form where the input is put in--->
<div class="input">
<form name="search" action="../Python/example.py" method="post">
<label for="input">enter in correct format:</label>
<input type="text" name="input" id="input">
<input type="submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
WARNING: This is only for amusement purposes--do not use in production.
Sticking to the Python standard library, here's an example to get you started.
from http.server import BaseHTTPRequestHandler, HTTPServer
class WebServer(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
funding_id = bytes.decode(self.rfile.read(content_length)).split('input=')[1]
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(f'<html><head><title>Funding ID Form Submit</title></head><body><p><b>Funding ID:</b> {funding_id}</p></body></html>', "utf-8"))
ws = HTTPServer(('localhost', 8080), WebServer)
ws.serve_forever()
You will need to change the action attribute of your HTML <form> to action="http://localhost:8080/" to see this in action.
Similarly, you can serve normal page requests by implementing the do_GET method for the WebServer class.
As already stated, this is for amusement/learning purposes. If you're putting something into production, look into learning something like Flask or Django

Selecting a value from drop down with span id in Selenium PYTHON

I have been trying this for a while and searched different forums but I couldn't find any snippet to do this.
I have a report in which options need to be selected through dropdown using Selenium in python. Below is the HTML structure
<div align="center"> Select Fruit</div>
<p align="center"><br>
<span id="0e6b87875e914a5f8d72bbee6844bea3" style="color: black; font-family: Arial; font-size: 13px; font-weight: normal; font-style: normal; width: 113px; display: inline-block;" class="sf-element sf-element-control sfc-property sfc-dropdown">
<div class="sf-element sf-element-dropdown" title="" style="position: relative; width: 100px;">
<div class="sf-element sf-element-icon" style="position: absolute; top: 1px; left: 91px; height: 17px; width: 17px;">
<svg width="17px" height="17px"><path d="M4,6 l7,0 l-3.5,3.5 z" fill="currentColor" stroke-width="1" transform="scale(1.1333333333333333,1.1333333333333333)" class="Down"></path></svg>
</div>
<div class="sf-element sf-element-text-box" style="display: inline-block; word-wrap: break-word; width: 83px;">(None)</div>
<select class="sf-dropdown-select" style="color: rgb(0, 0, 0); font-family: Arial; background-color: rgb(248, 248, 248);">
<option value="0" selected="selected">(None)</option>
<option value="1">Apple</option>
<option value="2">Mango</option>
<option value="3">Grapes</option>
</select>
</div>
</span><br></p>
I have tried different ways using css selector and XPath but nothing seems to work. Below is the code I tried
driver.find_element_by_xpath('//*[#id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[#value = "Mango"]')
Also different variants like options[2] and using css selector but it always give NoSuchElementException.
Can someone please share some insights on this?
Thanks
Add text()="Mongo" instead of #value="Mongo"
driver.find_element_by_xpath('//[#id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[text() = "Mango"]').click()
The dropdown is clearly within <Select> tag. Hence it would be convinient to use the Select Class as follows:
//import
from selenium.webdriver.support.ui import Select
//code block
selectOption = Select(driver.find_element_by_class_name("sf-dropdown-select"))
selectOption.select_by_visible_text("Mango")
When dealing with SELECT elements, there is a helper class, Select, that makes it a lot easier.
select = Select(driver.find_element_by_css_selector("#0e6b87875e914a5f8d72bbee6844bea3 select"))
select.select_by_visible_text("Mango")

Automatically play video on Mac

I've the following code in my webpage (Python/Django framework) to enable a video to play in the background.
HTML
<div class="video-container">
<div class="video-container-bg">
<video playsinline autoplay muted loop poster="{{page.image.url}}" id="bgvid">
<source src="{{page.video.url}}" type="video/mp4">
<source src="{{page.mac_video.url}}" type="video/webm">
</video>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8">
<div class="animation-element bounce-up">
<h1 class="page-title">{{page.page_title}}</h1>
<p class="strapeline">{{page.strapline}}</p>
<a class="butt" href="#about-us">Learn More</a>
</div>
</div>
</div>
</div>
</div>
</div>
CSS
video#bgvid {
position: absolute;
top: 50%;
left: 50%;
min-width: 100%;
min-height:100%;
overflow: hidden !important;
z-index: -100;
-ms-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
background: url() no-repeat;
background-size: 100%;
}
.video-container {
min-height: calc(100vh - 75px);
overflow: hidden !important;
position: relative;
}
.video-container-bg {
padding-top: 25vh;
color: #fff;
}
It works fine on everything except Safari where nothing plays. Why not? Is it something Apple have set to prevent? In fact, when I run Safari on Windows it's telling me it cannot play HTML5 video. Is that right?
I've solved this by saving the mp4 files in a lossless state. It now seems to work. I have no idea why

Categories