Python - Selenium - Problems navigating in iframes - python

I've been trying to log into a website using selenium. I have the following problem > i manage to open an iframe (it is correctly displayed after i click on the login button) but i've been unable to move into it. I've been trying to find it via it's Id and name but i somehow can't find it...
The line looks like:
driver.switch_to.frame(driver.find_element_by_id("frameid"))
I get an error "no such element". I checked 1000 times the id and it is correct! The frame i want to switch to is not inside another frame.
Any suggestions ?

It seem's that the id is somehow dynamically allocated but always starts with the same characters. I can't use a regex-based search apparently
In this case you can locate it with an XPath and starts-with() function:
frame = driver.find_element_by_xpath("//iframe[starts-with(#id, 'something')]")
driver.switch_to.frame(frame)
Or, with a CSS "begin with" selector:
frame = driver.find_element_by_css_selector("iframe[id^=something]")
driver.switch_to.frame(frame)

Related

Changing focus to a new window in Python/Selenium

Just like the title says, I am having trouble with getting my code to focus on a new window, using the "driver.switch_to_window".
My task is to:
Click button on the parent page > new window appears > click an "accept" button on the new window and continue with the rest of the code
driver.get("https:testpage.com/")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[2]/ul/li[1]/button").click()
#here the window appears
time.sleep(2)
driver.switch_to_window("windowName")
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[3]/button[2]").click() #here nothing happens
You can try to get the page source so that you are sure whether the driver has switched or not:
html_source = browser.page_source
It is possible that it has switched, but your element is not loaded or is in iframe. If your element is present, then you can try with different XPath that is relative, e.g. find the nearest id and find your element from it:
//div[#id='someid']//button[text()='someText']
I do not use absolute XPaths as I think they are too fragile.
Reference:
Python Selenium accessing HTML source
What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing?

How to find and click on specific element using selenium?

I'm starting in the world of python and I'm practicing creating things for my day to day. I decided to automate a routine task, but I can't get selenium to click on a specific place because it changes depending on how many columns I have and those columns always change order when I open a new login.
I thought about trying to find the specific xpath using the place name in the case "CORTEZ" and then find the "" specifies where I can click the button, but this "" is not inside the text "CORTEZ" it is in the same "line" or "group" (I don't know the correct term, forgive me) you will identify this in the image at the end of the text. I have no idea how to do this and I don't even know if it's possible as I said, I'm new to this world and I'm trying to learn little by little. I accept other subjects too
Most complete xpath I've ever tried too:
// table [# id = "accordionConvenio_Pane_0_content_GridView1"] / tbody / tr [3] / td / input
My current code:
def confirmarlocal(self):
try:
time.sleep(0.25)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,'//*[#id="accordionConvenio_Pane_0_content_GridView1_ctl05_btnParticipar"]'))).click()
except TimeoutException:
return
NOTE: I CANNOT DOWNLOAD THE HTML CODE THEN FOLLOW A PICTURE BELOW
Find the input based off it's class, title.
//input[#class='btnParticipar']
//input[#title='Confirmar']
If the problem consists in assembling the correct XPath to the desired tag, you can right-click the tag in the inspection menu, select "Copy" and then copy its XPath to get exactly the value you need, at least on Chrome.

Automate clicking button using python and selenium

I would like to automate clicking on "Refresh" button (See image "Refreshbutton") using python and selenium. I have tried to locate the button using xpath, id, class name but each time, I get the NoSuchElement exception, i.e. it cannot find the element. How can I locate the Refresh button? I don't care if it is by xpath, id or any other means. Thanks in advance!
HTML
RefreshButton
Find by XPATH try
Find By ID try
Class Name try
Python Code Snippet
The problem might not be the id/xpath but the presence of the button on the page. If the click is executed before the HTML page is fully loaded, the element might not be here yet. To make sure, you can use the WebDriverWait as follow:
element_xpath = '//*[#id="btnRefreshCameraList"]'
wait_time = 15 # wait for page to load for max. 15 seconds
WebDriverWait(
driver, wait_time).until(
EC.presence_of_element_located((By.XPATH, element_xpath))
)
)
driver.find_element_by_xpath(element_xpath).click()
Didn't have access to your site, so I couldn't try it out.

Selenium, dynamic content, chrome webdriver

I'm trying to understand how to scrape some dynamic webpages,
but I'm unable to get it to work.
(The page I'm currently playing with is betfair.com, which on their live betting
soccer page have a dynamic match statistics page. To see it in action, go to
betfair.com->Odds->LiveBetting, click on any soccer match.)
It is embedded inside two iframes, which I can access using:
frame1 = browser.find_element_by_xpath('//iframe[contains(#class, "player")]')
browser.switch_to.frame(frame1)
frame2 = browser.find_element_by_xpath('//iframe[contains(#id, "playerFrame")]')
browser.switch_to.frame(frame2)
I get an iframe back and can switch to it. So far so good.
However, when I now try to use 'browser' for anything,
I get no response what so ever.
Is there anything else one need to do in order to read form the content?
I'm trying something like:
browser.find_element_by_xpath("//div[contains(#id, 'in-game-stats')]")
The inner iframe above does contain the id. Also, if I try the steps above manual using chrome dev tools it does work. Any clues on why I get no answer to the above? Do I need to wait for something before it becomes available?
There is a third iframe underneath your frame2, select that before requesting for in-game-stats. All together,
frame1 = browser.find_element_by_xpath('//iframe[contains(#class, "player")]')
browser.switch_to.frame(frame1)
frame2 = browser.find_element_by_xpath('//iframe[contains(#id, "playerFrame")]')
browser.switch_to.frame(frame2)
You can try to get a better way of identifying this last iframe, here I am going to index it as the first iframe under iframe2.
frame3 = browser.find_element_by_xpath('//iframe[1]')
browser.switch_to.frame(frame3)
Now you can get the node that you were looking for:
browser.find_element_by_xpath("//div[contains(#id, 'in-game-stats')]")

How to take elements by Selenium which created by JS script

I trying automating testing with Selenium (python bindings), specifically want to log in on tokmonet.zendesk.com.
I create a script which takes email field, password field and sign in button by id.
But when I ran script it fails with
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"user_email"}
Inspecting page with Firebug I see these elements. But when trying to get them with Firefinder it couldn't.
So, I perform
html_source = driver.page_source
print(html_source)
and get the only
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
When I check page source it contains only js scripts and no mark up.
Please advice how I could handle this elements?
I see that elements that you are trying to log in are in an iframe in tokmonet.zendesk.com and so you are not able to get the elements. To handle such situation try to switch to the iframe first and then get the elements. Here's how to do it in Java -
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
(new WebDriverWait(driver, 20))
.until(ExpectedConditions.presenceOfElementLocated(By.id("user_email"))).sendKeys("username");
//Similarly you can get other elements too
You similarly implement it in other languages. Hope this helps.
You need to switch to the IFRAME, then send_keys() to the element which you can find by ID. Don't forget to switch back to the default content if you need to access elements outside the IFRAME.
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_id("user_email").send_keys("username")
driver.find_element_by_id("user_password").send_keys("password")
// do whatever else
driver.switch_to.default_content()

Categories

Resources