Python Selenium .click() Not Working but Server Response is 200 - python

I'm having a hard time coming up with a solution. I'm writing code in Python using the Selenium library to pull reports from a site we utilize. In order to access the reports, it has to click on a link to expand so the code can find the report names. The issue I'm running into is the code is running fine and clicking on the link, but the link isn't expanding to reveal the reports. Debugging is saying that the link is found and clicked with a post response of 200, but nothing is happening. Due to the nature of the response code saying everything is fine, I'm at a loss of why the folder isn't expanding. Due to the nature of the site, I cannot share too much but have attached my code that I can share along with screenshots.
wait = WebDriverWait(driver_chrom, 30)
def Try_Wait(args, thing_string):
try:
wait.until(EC.presence_of_element_located(args))
logger_write.debug("found element "+thing_string)
#print("found args for ", thing_string)
return
except:
Exit_LogOut()
driver_chrom.quit()
sys.exit()
def Button_Click(args, thing_string):
try:
driver_chrom.find_element(*args).click()
logger_write.debug("Found button click"+thing_string)
#print('clicking button for ', thing_string)
return
except:
#print('did not click button ', thing_string)
logger_write.error("Did not find button "+thing_string)
return
thing_string = 'Opening folder for reports'
Try_Wait((By.XPATH,'//div[#id="folder1"]/table/tbody/tr//td[a[starts-with(#href, "javascript:clickOnNode(") and contains(#href, "1") and contains(text(),"Standard")]]'), thing_string)
Button_Click((By.XPATH,'//div[#id="folder1"]/table/tbody/tr//td[a[starts-with(#href, "javascript:clickOnNode(") and contains(#href, "1") and contains(text(),"Standard")]]'), thing_string)
This is what it looks like after code above runs
This is what it should look like so that reports are loaded into the html
Here is the inspect screenshot:

Try following xpath should work for you.
//a[text()="Standard"]
Instead this
//div[#id="folder1"]/table/tbody/tr//td[a[starts-with(#href, "javascript:clickOnNode(") and contains(#href, "1") and contains(text(),"Standard")]]
Or use Link_Text since it is anchor rag
Try_Wait((By.LINK_TEXT ,"Standard"), thing_string)
Button_Click((By.LINK_TEXT ,"Standard"), thing_string)

Related

Can't find a way to upload an image (interact with the element)

So i have a code which logins to Facebook and fills everything needed to publish an item in marketplace, but I've been struggling in uploading images.
First of all there is no input tag inside the 'parent tag' (div class="aov4n071") (the first div):
However when i try this line of code:
ele = wait.until(EC.presence_of_element_located((By.XPATH,'//*[contains(text(), "AƱadir fotos")]')))
ele.click()
The element does get clicked without any error, and the dialog where you need to browse your folder manually shows up. AS EXPECTED. However when i send_keys() to ele it shows:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <span class="a8c37x1j ni8dbmo4 stjgntxs l9j0dhe7 ltmttdrg g0qnabr5 oi732d6d ik7dh3pa d2edcug0 qv66sw1b c1et5uql muag1w35 ew0dbk1b jq4qci2q a3bd9o3v lrazzd5p oo9gr5id"> is not reachable by keyboard
Which is understandable because it is not an input element. But where the hell is the input tag??
I found out that there are two input classes that are right above the 'parent' class i talked about in the first picture. But these elements are not like the others, when i put my cursor on them, they don't behave like the other elements. It seems like they are invisible. (look how they are not colored like the other ones)
I tried this line of code:
ele = wait.until(EC.presence_of_element_located((By.CLASS_NAME,'mkhogb32')))
ele.send_keys(var)
But it gives me this error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <div class="eopy0mj9 ina5je9e kr520xx4 j9ispegn poy2od1o b5wmifdl mw227v9j n7fi1qx3 dya21fl7 a47luoao sbevj9st ev399l9o iwynql29 kuz35qqg mkhogb32"> is not reachable by keyboard
But when i try this:
ele.click()
I get the following error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <div class="eopy0mj9 ina5je9e kr520xx4 j9ispegn poy2od1o b5wmifdl mw227v9j n7fi1qx3 dya21fl7 a47luoao sbevj9st ev399l9o iwynql29 kuz35qqg mkhogb32"> could not be scrolled into view
I don't know much about HTML but i can tell these are the tags that will allow me to upload pictures, but why do the behave different?
What can i do do finally upload pictures?
Are the input tags in another frame?
this code is running good
POST="c:\\maphoto.jpg"
l=navigateur.find_elements_by_tag_name('input')
for g in l:
if g==navigateur.find_element_by_xpath("//input[#type='file'][#class='mkhogb32']"):
g.send_keys(POST)
time.sleep(5)

Test case for login result using python in Selenium

I am writing a sample test case to display the login result of the page - http://testing-ground.scraping.pro/login
When I run the test case, all the test cases pass and none of the logins are rejected. Here is my code -
for r in range(2,row+1):
uname=utils.readData(path,'new',r,1)
password=utils.readData(path,'new',r,2)
driver.find_element_by_xpath("//*[#id='usr']").send_keys(password)
driver.find_element_by_xpath("//*[#id='case_login']/form/input[3]").click()
if driver.find_element_by_xpath("//*[contains(text(), 'ACCESS DENIED!')]"):
print("fail")
else: print("Pass")
utils.writeData(path,'new',r,3,"Test Fail")
utils is my module to read the data stored in the excel file.
Please tell me what I did wrong in this code, Thanks in advance :)
You were not setting the username and password properly. You also need to search for the text in an h3 tag since the search text always appears in the main page. Also use find_elements_by_xpath (plural) when searching for text to prevent exceptions.
This code worked for me:
driver = webdriver.Chrome('./chromedriver')
target_url = 'http://testing-ground.scraping.pro/login'
driver.get(target_url)
uname="admin"
password="12345"
driver.find_element_by_xpath("//*[#id='usr']").send_keys(uname)
driver.find_element_by_xpath("//*[#id='pwd']").send_keys(password)
driver.find_element_by_xpath("//*[#id='case_login']/form/input[3]").click()
if len(driver.find_elements_by_xpath("//h3[contains(text(), 'WELCOME')]")):
print("Pass")
else:
print("Fail")

Python + selenium download image without extension

I'm using python 3 with selenium, I have to download an image
HTML:
<img id="labelImage" name="labelImage" border="0" width="672" height="456" alt="labelImage" src="/shipping/labelAction.handle?method=doGetLabelFromCache&isDecompressRequired=false&utype=null&cacheKey=774242409034SHIPPING_L">
Python code:
found = browser.find_element_by_css_selector('img[alt="labelImage"]')
src = found.get_attribute('src')
urllib.request.urlretrieve(src, 'image.png')
that image file is empty, if I try to switch extension to html, shows me message below:
"We're sorry, we can't process your request right now. It appears you don't have permission to view this webpage"
The error you recieve when attempt to download comes from the fact the urllib call is a brand new session for their server - it does not have the cookies and authentication your browser does. E.g. it is the same as if you open incognito mode in the browser, and paste in the address bar the src attribute - for the server you are a new client, that hasn't fill in the form, logged in, etc.
You may want to try something else - in the selenium/the browser session, taking a screenshot of just the <img> element. That op is with variable success, Chrome for instance added support for it only recently, and in some situations it fails:
found = browser.find_element_by_css_selector('img[alt="labelImage"]')
try:
found.screenshot('element.png')
except Exception as ex: # FIXME: anti-pattern - I don't recall the exact exception - when you run the code, change it to the proper one
print('The correct exception is {}'.format(ex))
browser.get_screenshot_as_file('page.png')
If taking the element's screenshot fails, you'll get one of the whole page - which you can then trim to the element.

python selenium not updating pop up window url

I am trying to scrape webpages using python and selenium. I have a url which takes a single parameter and a list of valid parameters. I navigate to that url with a single parameter at a time and click on a link, a pop up window opens with a page.
The pop window automatically opens a print dialogue on page load.
Also the url bar is disabled for that popup.
My code:
def packAmazonOrders(self, order_ids):
order_window_handle = self.driver.current_window_handle
for each in order_ids:
self.driver.find_element_by_id('sc-search-field').send_keys(Keys.CONTROL, "a")
self.driver.find_element_by_id('sc-search-field').send_keys(Keys.DELETE)
self.driver.find_element_by_id('sc-search-field').send_keys(each)
self.driver.find_element_by_class_name('sc-search-button').click()
src = self.driver.page_source.encode('utf-8')
if 'Unshipped' in src and 'Easy Ship - Schedule pickup' in src:
is_valid = True
else:
is_valid = False
if is_valid:
print 'Packing Slip Start - %s' %each
self.driver.find_element_by_link_text('Print order packing slip').click()
handles = self.driver.window_handles
print handles
try:
handles.remove(order_window_handle)
except:
pass
self.driver.switch_to_window(handles.pop())
print handles
packing_slip_page = ''
packing_slip_page = self.driver.page_source.encode('utf-8')
if each in packing_slip_page:
print 'Packing Slip Window'
else:
print 'not found'
self.driver.close()
self.driver.switch_to_window(order_window_handle)
Now I have two questions:
How can I download that pop up page as pdf?
For first parameter every thing works fine. But for another parameters in the list the packing_slip_page does not update (which i think because of the disabled url bar. But not sure though.) I tried the print the handle (print handles) for each parametre but it always print the same value. So how to access the correct page source for other parameters?

python webdriver os window

I need to upload a file using Python and Selenium. When I click the upload HTML element a "File Upload" window is opened and the click() method does not return since it waits to fully load the page. Therefore I cannot continue using pywinauto code to control the window.
The first method clicks the HTML element (an img) to upload a new file:
def add_file(self):
return self.selenium.find_element(By.ID, "add_file").click()
and the second method is using pywinauto to type the path to the file and then click open
def upload(self):
from pywinauto import application
app = application.Application()
app.connect_(title_re = "File Upload")
app.file_upload.TypeKeys("C:\\Path\\To\\FIle")
app.file_upload.Open.Click()
How can I force add_file method to return and to be able to run the upload method?
Solve it. There was an iframe dealing with the upload but was hidden and didn't see it in the first place. The iframe contains an input of type file also hidden. To solve it make the iframe visible using javascript:
selenium.execute_script("document.getElementById('iframe_id').style.display = 'block';")
then switch to the iframe and make the input visible also:
selenium.switch_to_frame(0)
selenium.execute_script("document.getElementById('input_field_id').type = 'visible';")
and simply send the path to the input:
selenium.find_element(By.ID, 'input_field_id').send_keys("path\\\\to\\\\file")
For windows use 4 '\\\\' as path separator.

Categories