Getting the frame id of the iframe element - google-chrome-extension

I would like to use the executeScript function and using frameId, however, I cannot get the frameId from the iframe element. By getting id, it will be a string, which does not match the requirement of using the executeScript.
So far I can get the element in the content page and wish to inject the code to reach the element inside the frame. Currently, I am using allFrame:true, but I wish to improve into injecting script for the only frame.
Is there any idea?

Related

Getting error Element not interactable on trying to send Page Down keys to div

I am trying to scroll Telegram using selenium in python. In the attached screenshot I have shared I have selected 'Members' as the element to send Keys.PAGE_DOWN as it is stick all the time to top and is static while scrolling so it should be visible all the time and can be the perfect element to send Keys.PAGE_DOWN to.
But on sending page_down I get error 'Element not Interactable'.
Any suggestions what I am doing wrong?
I have attached the script and screenshot.
I am using python 3.10 and selenium latest version.
`driver.find_element(By.XPATH, "//*[#id='RightColumn']/div[2]/div/div/div[2]/div[2]/div[1]").send_keys(Keys.PAGE_DOWN)`
I have tried all the answers currently available on the internet and they don't work here. This looks like some complex issue.
I think Selenium is throwing the right error message as this div is not an interactable element and you are trying to send keystrokes into the element.
Another approach for scrolling is using Javascript commands.
Find an element locator you need to scroll to.
(Ex: if you need to scroll to the bottom find the element at the bottom)
Use the below code to scroll
# Find the element in the page to scroll to
element = driver.find_element_by_xpath("//element/at/bottom/of/the/page")
# Fire javascript command to scroll in to view
driver.execute_script("arguments[0].scrollIntoView();", element)

Selenium: failing to find element by XPATH Python

I am a little bit new to programming but python really made get into it. I am trying to create a programm that automatically checks for updates in a website. I've successfully implemented the neccessary code to call the page of enrollment but yet there is one element that cannot be located. Since I have to do it for multiple courses and iterate throught them there is no specific id, I've tried to find it by title but also this didn't work.
Is there a way you can locate the button with the title "enroll".
I've tried
driver.find_element_by_xpath("//a\[#title ='enroll']").click()
but this didn't work and I always get
NoSuchElement
error.
The XPATH for the button is simply: //*\[#id="id572"\]
Here is the part of the HTML code:
From the screenshot of HTML code you provided, the element is <button>, not <a>.
Try this xpath expression //button[#title='enroll']
This should do it if it's not in any iframes. Just grab the button whose title is enroll and click. Your css selector was an a tag and it might get id dynamically.
driver.find_element_by_css_selector("button[title ='enroll']").click()

Sending text to text box using Selenium in Python

I am trying to create a script within Python using Selenium to open a social media page (Tumblr), input user login credentials, and make "quote" posts with a random sentence generator. I am able to input the credentials and select the create post icon using xpaths (driver.find_element_by_xpath) from the website, but when trying input text for the post with the xpath
//*[#id="redpop_iframePostForms"]/div[3]/div/div/div/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div/div[1]/p
using the
self.driver.find_element_by_xpath('//*[#id="redpop_iframePostForms"]/div[3]/div/div/div/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div/div[1]/p').send_keys("It worked!!")
I receive the error
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#id="redpop_iframePostForms"]/div[3]/div/div/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div/div/div[1]"}
I have tried using the other div classes within the list of divs, but I cannot find the correct one. I have also tried using the CSS selector with the code
driver.find_elements_by_css_seletor("div[#aria-label='Quote']").send_keys("It worked!!!")
but that provided the error
Message: invalid selector: An invalid or illegal selector was specified
Any direction on where to go from here?
Thanks!
For no such element exception
From your xpath, i can guess that element is present in an iframe. First switch to that frame and then find element you want to interact with.
For invalid selector message
General Syntax for locating element with css selector is :
tagname[attributeName=‘attributeValue’]
You don’t need to use # with attributeName.
Also, you are fetching all the div elements present in the DOM with aria-label=‘Quote’ and sending keys which is not right. If you want to perform any action on elements, you need to put them in list and then iterate over it. Hope this helps.

Python Selenium - Iframe Dynamically Change Id and Name But Src is Same

Issue : - Unable to detect the iframe and switch to iframe as the iframe id and name dynamically changes at each time it loads but the src remain the same.
Unable to identify the xpath as well. Found iframe tag using driver.find_element_by_tagname("iframe") but cant view the tag <iframe> in the HTML of the page source. Therefore unable to find the xpath by right click and iframe tag.
Already tried to find the number of frame size using 'frame.size' but since its not callable, it provide answer in dict which is 'height:0,weight:0'
Require to switch to this iframe and work with the element inside the iframe which is to
1) enter text field into the iframe
2) select radio button in the iframe
Unable to select element after switch to iframe
Screenshot of the code
enter image description here
By default, each frame is assigned an integer number. So you can always do:
driver.switchTo().frame(0)
or
driver.switchTo().frame(1)
You can wait until frame to be loaded as given below.
wait = WebDriverWait(driver, 300)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe')))
I was able to answer this, by determining the number of iframe. I've used 'find_element's'_by_tagname('iframe') instead of find_element_bytagname('iframe') which was able to find more than 1 iframe if exist in the page source.
This provides me total 3 iframe tag. Then i proceed to loop into the iframe and find the attribute of the each iframe such as id,name and src.
Then i noticed one of the iframe src, refers to the web address that has the element which i was looking for. Then i proceed to switch to that particular frame using if statement.
Once switch to the iframe, i proceed to get the element.
This solved on how to switch between frame using python-selenium and detect element upon switch the frame.
The key here is to detect on which frame does the element exist within and able to switch to that frame. The frame could be either nested within one frame or list of frame in the main window.
Upon detect and switch the frame. Then would be able to locate the element.
This solves the challenges

chrome extension inject in frameset code

i develop some extension for google grome
i inject at document_end event my js (+jquery)
i set in manifest allFrames: true
my match url has frameset
my goal get element by id inside one of frame
i do
//wait for load my frame
$("frame[name='header']).load(function() {
//here I need get element by id inside this frame
});
how to do this properly?
PS: my frame is from the same domain
You dont need to do document load, I assume your doing this from a content script. Just place the "run_at" manifest to be document_end and within your content script you check if the current URL is that frame page, then you will be in that Domain.
Something like this:
if(location.href == 'http://someweb.com/path/to/page.html') {
var foo = document.getElementById('foo')
Something like that will get you started.

Resources