How to identify and switch to the frame source in Selenium? - python

I have been using the Selenium and Python2.7, on Firefox.
I would like it to work like the following, but I do not know how to write code.
Driver.get('https://video-download.online')
Url='https://www.youtube.com/watch?v=waQlR8W8aTA'
Driver.find_element_by_id("link").send_keys(Url)
Driver.find_element_by_id("submit").click()
time.sleep(5)
#[Click]lowermost(highest quality) radio button
#[Click]Proceed button

You are able to get frame locator value in HTML code which is specified in iframes tag. u can move up from where your locator value is existed in HTML code.
This function is suitable:
def frame_switch(css_selector):
driver.switch_to.frame(driver.find_element_by_css_selector(css_selector))
If you are just trying to switch to the frame based on the name attribute, then you can use this:
def frame_switch(name):
driver.switch_to.frame(driver.find_element_by_name(name))

Related

Click on dynamic dropdown element with selenium and python

I try to click on dropdown options using selenium and python. The problem is that the dropdown values are different each time I open the browser therefore, i can not use the HTML tags from the webpage.
I'm wondering if there's any way of clicking the values?
I tried to use this line of code using answered provided by Sers in this case as an example How to select/click in a dropdown content using selenium chromewebdriver / python but I'm not sure how should I change/compose the class here.
att4 = WebDriverWait(driver, 2).until(ec.visibility_of_element_located((
By.CLASS_NAME, f"div.awsui-select-option awsui-select-option-selectable[title='{l}']"))).click()
l is for each option in my dropdown, I need to click on each of them
You are using a wrong, non-stable locator.
Try this:
att4 = WebDriverWait(driver, 2).until(ec.visibility_of_element_located((By.XPATH, "//div[#data-value='Bambino']"))).click()
Or
att4 = WebDriverWait(driver, 2).until(ec.visibility_of_element_located((By.XPATH, "//div[#title='Bambino']"))).click()

Unable to switch to iFrame using Selenium

I'm currently trying out Selenium for Python to do some web automation.
I got stuck a the checkout where you have to enter your credit card data.
I'm aware that I have to switch to iFrame to locate the input box.
But I was never able to switch the frame.
I tried it for ID or for example for this:
time.sleep(4)
frame = self.driver.find_element_by_xpath("//iframe[#name= '__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834']")
self.driver.switch_to.frame(frame)
name_input = self.driver.find_element_by_id("card-holder")
name_input.clear()
name_input.send_keys(credit_card.NAME)
I'm always getting "Unable to locate element : {"method":"xpath","selector":"//iframe[#name= '__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834']"}".
I added a pic from the Source Code.
Source Code from the Webpage
I'm thankful for any advise!
Best, Jannic
Possibly id & name attribute is dynamic.
Try with starts-with() or use other attribute like class to identify the iframe.
frame = self.driver.find_element_by_xpath("//iframe[starts-with(#name, '__privateHeidelpayFrame--heidelpay-holder-iframe-')]")
self.driver.switch_to.frame(frame)
OR
frame = self.driver.find_element_by_xpath("//iframe[#class='heidelpayUIIframe']")
self.driver.switch_to.frame(frame)
Have you tried using a simpler API to find the iFrame?
Does
self.driver.find_element(By.NAME, "__privateHeidelpayFrame--heidelpay-holder-iframe-1616489954834'")
Return anything?

Extracting hidden element in Selenium

I have an element of type hidden in an iframe. I am wondering if there would be any way to get this value as I am using selenium. More specifically it is a captcha field. I've tried pulling it with something along the lines of
#!/usr/bin/env python
from selenium import webdriver
driver=webdriver.Chrome(chrome_bin_path)
driver.get('http://websitehere.com')
print driver.find_element_by_xpath('//*[#id="recaptcha-token"]').text
but because of it's hidden nature it returns nothing.
Below is a snippet of the source.
Highlighted is the string of interest. (value)
driver.switch_to_frame('undefined')
token_value = driver.find_element_by_id('recaptcha-token').get_attribute('value')
driver.switch_to_default_content()
Moving between windows and frames.
Use this method
hidden_text = element.get_attribute("textContent")

Python-Selenium-How to find out if it's a window or iframe or popup

I am trying to automate some test case using Python Selenium Webdriver. I click on a button which opens up a new window/iframe/popup. Which needs some data to be be added in text box and choose some from the drop box. I have two questions,
How would I find if its a window/iframe/popup from the firepath in Mozilla?
How to get the handle?
This is the xpath and content I see in firepath when I select the new window/iframe/popup(image attached),
html/body/div[11]
I have the code here,
zenoss_url = "http://xxxxx/"
xpath_uname = "html/body/form/div/div/input[1]"
xpath_pwd = "html/body/form/div/div/input[2]"
xpath_submitButton = "html/body/form/div/div/input[3]"
xpath_eventsTab = "html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a"
driver = webdriver.Firefox()
driver.get(zenoss_url)
handle = driver.find_element_by_xpath(xpath_uname)
handle.send_keys(zenoss_uname)
handle = driver.find_element_by_xpath(xpath_pwd)
handle.send_keys(zenoss_pwd)
driver.find_element_by_xpath(xpath_submitButton).submit()
driver.implicitly_wait(10)
driver.find_element_by_xpath("html/body/div[7]/div/div/div/div[2]/ul/li[2]/div/div/a").click()
driver.find_element_by_xpath("html/body/div[3]/div/div/div/div[1]/div/div/div[1]/div/div/div[7]/em/button").click()
driver.implicitly_wait(5)
window = driver.switch_to_window(?????)
I am not sure what "name" i should replace with ?????
Appreciate any help.
Your path of html/body/div[11] is only the xpath from the scope of the frame. When navigating to frames and subframes there are a few approaches you can take; from looking for the index of the frame to looking for the name of the frame. If you use firepath to inspect an element inside the frame, it will only give you the xpath for the element the scope of that frame. Frames work like boxes inside one another; in order to get to the second box, you have to open the first box and look inside.
//In C#
driver.switchTo().Frame("FirstFrameFound");
//To close the box again and get back to the overall main box.
driver.switchTo().defaultContent();
If your 2nd frame is a child of the first frame, you have to dive into the first, and then switch to the 2nd to interact with elements inside.
//In C#
//Switch to the first frame
driver.switchTo().Frame("FirstFrameFound");
//Switch to the second frame
driver.SwitchTo().Frame("SecondFrame");
//Now look for your element that you wish to interact with.
driver.FindElement(By.XPath("//html/body/div[11]"));
//To close the box again and get back to the overall main box.
driver.switchTo().defaultContent();
Please look into this post here for more information.
How to navigate a subframe inside a frameset using Selenium WebDriver?
If you are switching to a window there are multiple approaches as well. My preferred approach is this method:
//When dealing with new window and the parent.
//Switch to the last window that opened
driver.SwitchTo().Window(driver.WindowHandles.Last());
//Perform your actions
driver.Close();
//Switch back to the first window
driver.SwitchTo().Window(driver.WindowHandles.First());

Selenium Webdriver with Python - obtaining the string or contents in between html tags

I'm new to Selenium's webdriver and Python. I know about this article about getting the HTML source, but I don't want the entire HTML line for a DOM object, just the content in between the tags. I also looked at webdriver source code for help on finding this button DOM object
for example:
<button id = "picview">Pic View</button>
How do I just get "Pic View"?
Also, using get_attribute("button id"), How would I get this specific button id as there are multiple buttons on the page with button id?
For example:
picbox_elem_attr = picbox_elem.get_attribute('button id')
print picbox_elem_attr
How do I ensure that picbox_elem_attr variable is set to the "picview" button and not some other button?
I don't have a
driver.find_element_by_name("xxx")
or a
driver.find_element_by_id("aaa")
to use to find this button.
To get text of an element use the text property. For example:
text_inside_button_id = driver.find_element_by_id("picview").text
Here is some additional documentation to help you with the webdriver binding library.
http://goldb.org/sst/selenium2_api_docs/html/
I forgot about xpath. Oops!
driver.find_element_by_xpath('//*[#id="picview"]')
and then you can right-click the object and use xpath within the dev tools.

Categories

Resources