Selenium python iframe - python

so I want to do an automated testing on an iframe (input image). Here is what I have done so far:
WebDriverWait(driver, 1000).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element("xpath","//div[#id='el_banner_url_upload']//iframe")))
upload_image = driver.find_element("xpath", "//input[#name='images']")
upload_image.send_keys(str(d_image))
driver.switch_to.default_content()
For the flow:
I click on a button inside the iframe
It opens my windows folder, then I select a picture I want to upload
The image uploaded and it gives a preview + a link to the image that is generated automatically by the website is shown on the field below the displayed image
Here is the link to the screenshot of what I'm talking about:
Before uploading the image
After uploading the image
I got 2 problems right now,
After running the script, it works well (didn't give any error on the command). The "upload" button is gone now. But, the image is not shown on the display and there are no link generated. As shown in this screenshot. Tried inspect it, and found this error on the console:
Uncaught TypeError: Cannot read properties of undefined (reading 'showQuality')
at HTMLFormElement.add (pic-embed.js:173:35)
at $.<computed>.<computed>._trigger (jquery.ui.widget.js:489:13)
at File.<anonymous> (jquery.fileupload.js:846:31)
at Function.each (jquery.js:4:5347)
at $.<computed>.<computed>._onAdd (jquery.fileupload.js:840:15)
at $.<computed>.<computed>._onAdd (jquery.ui.widget.js:105:25)
at Object.<anonymous> (jquery.fileupload.js:1016:26)
at c (jquery.js:4:26036)
at Object.add [as done] (jquery.js:4:26346)
at Object.always (jquery.js:4:27212)
The iframe would randomly not refreshed correctly, this too I couldn't figure out how. If this happens, the script couldn't run. Here is the screenshot of when the iframe didn't refresh correctly
Are there any solution to this?

You should use Selenium expected conditions. Don't just find elements and click them, wait for them to be enabled or clickable:
driver.wait.until(ExpectedCondition.element_to_be_clickable((By.XPATH, "myXpath"))).click()
driver.wait.until(ExpectedCondition.element_to_be_clickable((By.CSS, "myCSS"))).click()
When you try to click on an element before it is actually clickable - you are going to get errors.

Related

How to click on an element with Selenium without using Driver.get?

So I am trying to write a code to automatically join a google meet. So I need to sign into my google account, which runs all fine but when I get to "Verify that it's you" (image 1), and I try to use driver.get it takes me back to the original sign-in page (image 2)
How can I click on the "Continue" button without using driver.get/without getting taken back to the original sign-in page. I know that all I have to do is click on the "Continue" because when I do it manually it works perfectly. Pressing "Tab" and then "Enter" would also work, but it seems you need to use driver.get as well. Thank you
so i think you don't understand driver.get() usage. it is used only to make the browser browse to a specific link which you can specify before hand. what you have to do here is open the google meet page manually and go to your required page where you want to click the button. right click on the button and click on inspect. then go to the higlighted code and locate the button you want and right click on copy and select xpath from the list. now coming to your code.
add this line:
element=driver.find_element_by_xpath('_xpath')
element.click()
replace _xpath with the long string you copied (which is the xpath of the button you want to click).
this is the way to click on buttons or text boxes or anything you name it in selenium.
**beware of one thing. don't make your code click immediately. if the page does not fully load and the click is made then the 1st line will throw an element not found error. put some kind of delay till page loads or use a while loop and exception handling to wait till the element is found
This one should work for you
userName = driver.find_element_by_xpath("//button[#name='username']")
driver.execute_script("arguments[0].click();", userName)

Python - click on sharepoint CSS menu to download to excel with selenium .ms-core-menu-title

I'm trying to automate the following process
Accesing a Sharepoint site
Loading some input data
Running a query that retrieves some data
Download it to excel through a menue (Actions, Download, Excel)
I was successful with steps 1 to 3 and made it work perfectly but I'm stuck with the forth one.
I recorded the 3 clicks in the menu needed to download to excel getting the following ids:
css=#m_sqlRsWebPart_RSWebPartToolbar_ctl00_RptControls_RSActionMenu_ctl01_t > .ms-core-menu-arrow
css=#m_sqlRsWebPart_RSWebPartToolbar_ctl00_RptControls_RSActionMenu_Export > .ms-core-menu-title
css=#mp1_1_3_Anchor > #m_sqlRsWebPart_RSWebPartToolbar_ctl00_RptControls_RSActionMenu_EXCELOPENXML
Then, I created the following code:
driver.find_element_by_css_selector(".ms-core-menu-arrow").click() # click on Actions menu
driver.find_element_by_css_selector('.ms-core-menu-title').click() # click Excel
driver.find_element_by_css_selector('#mp1_1_3_Anchor > #m_sqlRsWebPart_RSWebPartToolbar_ctl00_RptControls_RSActionMenu_EXCELOPENXML').click() # click Excel
When I run it, it clicks on Action menu (first step), but then it gets stuck with the following message:
"Unable to locate element: {"method":"css selector","selector":".ms-core-menu-title"}
I don't understand why the code is able to click on the first button ".ms-core-menu-Arrow" but not on the second one ".ms-core-menu-title"
I tried to skip the second row of code and go directly to the third one for downloading to excel but it fails the same way in identifying the css element.
Any help?
Thanks!!
I don't fully understand how it works (I'm new on it), but I tried again to record the sequence with Selenium in Chrome and instead of returning CSS button codes, it returned "ids"
I finally targeted it with the following code and it worked!!!
driver.find_element_by_css_selector(".ms-core-menu-arrow").click() # click on Actions menue
time.sleep(6)
driver.find_element_by_id('mp1_0_2_Anchor').click() # click Download
driver.find_element_by_id('mp1_1_3_Anchor').click() # click Excel
I have to add 6 seconds after the first menue to load so the following buttons could be pressed so anyways your answer helped me to figure it out!
Many thanks!!

Taking full page screenshot in chrome store SELENIUM PYTHON

I'm trying to save a full-page screenshot of a chrome store page, using selenium, and python 3.
I've searched online for different answers and I keep getting only the "header" part, no matter what I try. As if the page doesn't scroll for the next "section".
I tried clicking inside the page to verify it's in focus but that didn't help.
Tried answers with stitching and imported Screenshots and Image.
my current code is:
ob = Screenshot_Clipping.Screenshot()
driver2 = webdriver.Chrome(executable_path=chromedriver)
url = "https://chrome.google.com/webstore/detail/online-game-zone-new-tab/abalcghoakdcaalbfadaacmapphamklh"
driver2.get(url)
img_url = ob.full_Screenshot(driver, save_path=r'.', image_name='Myimage.png')
print(img_url)
print('done')
driver2.close()
driver2.quit()
but that gives me this picture:
What am I doing wrong?

Python, Selenium Webdriver: The element is not visible for Firefox.

I'm trying to iteract with a webelement on a page that has edit link that opens a popup. In opened popup I have simple input field and Apply/Cancel buttons. In my script I do the following to enter some text to the input field:
def enter_text(self, text, action):
if self.is_element_present(self._input_locator):
self.selenium.find_element(*self._input_locator).send_keys(text)
if action == 'Apply':
self.selenium.find_element(*self._apply_button_locator).click()
elif action == 'Cancel':
self.selenium.find_element(*self._cancel_button_locator).click()
When I run my script in Chrome - everything works fine, all webelements are found and input text is entered to the field. But when I run exact same script in Firefox - it opens popup window (which means it became visible for Webdriver) with input field and 2 buttons but the text is not getting entered to the field which causes error:
ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with'
Why can this happen if the popup is actually opened (and I can see it) but Webdriver tells that it is not visible? Also, I put several sleeps just to get sure that popup loaded and then the text is entered but it did not help.
Any help will be appreciated.
Two ideas:
1: make sure your HTML is valid, eg by running it thru the w3c validator. broken html is a common cause of different behaviours in different browsers.
2: have you definitely switched selenium over to the popup, using, eg:
for handle in self.selenium.window_handles:
# identify popup window's handle somehow
self.selenium.switch_to_window(handle)
As an aside, if you haven't come across selenium.implicitly_wait(3), it's dead useful as a way of avoiding time.sleeps and wait-for constructs..
Witch Firefox version and Webdriver version are you using?
Please try the following:
Update Webdriver to the last version
If you are using firefox 17-18, downgrade it to lower version (I guess FF12 will work)
Please try that and tell me what happens,
Regards

How to save an image by selecting "save image as..." in a context menu using Selenium Webdriver (Python)

I'm trying to use selenium webdriver to save a specific image to a directory. I was looking to do this by simulating a right click on the img element and selecting "save image as...". With the following code I can open the context menu, but I'm unable to select the correct option.
browser = WebDriver(executable_path=CHROMEDRIVER_PATH)
browser.get(URL)
img = browser.find_element_by_tag_name('img')
ActionChains(browser).context_click(img).perform()
I also tried:
ActionChains(browser).context_click(img).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
and using a shortcut ('v' seems to select "save image as...")
ActionChains(browser).context_click(img).send_keys('v').perform()
The image does not have a direct URL because it's a captcha image that is reloaded randomly on each click. The only way I found, for me to be able to process it, is to save it on the disk first (using "save image as..."). Saving the entire page does not save this specific image so it won't work as well.
Any ideas?
If it's a captcha you're after, you're probably better off just taking a screenshot.
driver.save_screenshot('screenshot.png')
I was having the similar problem and just now got a shortcut to save a image by using save as---
Step-1-right click on an image you wish to save
Step-2- press v.
Step-3-Enter to the directory window to save
Since i am a java programmer thats why i was not able to paste a code accordingly( question asked for python).
As suggested by kreativitea, a screenshot would be the way to go because CAPTCHA's were designed to prevent scripts from doing what you're trying to do. CAPTCHA's are meant to prevent session re-use. Here is a page describing session re-use.

Categories

Resources