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()
Related
With selenium in python, I want to collect data about a user called "GrahamDumpleton" on the website below:
https://github.com/GrahamDumpleton/wrapt/graphs/contributors
And this is the block I want to locate with the user name "GrahamDumpleton":
How to locate this block using selenium?
Thank you.
This can be clearly done with XPath since XPath is the only approach supporting locating elements based on their text content.
So, that user block element can be located with the following XPath:
//li[contains(#class,'contrib-person')][contains(.,'Graham')]
In case you want only the header part of that block this XPath can be used:
//h3[contains(#class,'border-bottom')][contains(.,'Graham')]
So, Selenium code returning those elements can be correspondingly
driver.find_elements(By.XPATH, "//li[contains(#class,'contrib-person')][contains(.,'Graham')]")
And
driver.find_elements(By.XPATH, "//h3[contains(#class,'border-bottom')][contains(.,'Graham')]")
I encountered a challenge while trying to switch to a certain iFrame using selenium (in python if it matters)
I have this inner iFrame which resides in this weird HTML component
The iFrame is in this macroponent element:
and I cannot fetch it via selenium. I tried using driver.switch_to_frame("gsft_main") and also tried by using xpaths. It seems that first I need to enter this "macroponent" element before.
Any idea will be appreciated!
Refer this link for one of the example using CSS selector to access iframe in Shadow-dom:
How to access element inside shadow root in Robot framework
This link has full explanation to work with shadow-dom using python, java, javascript
https://titusfortner.com/2021/11/22/shadow-dom-selenium.html
I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.
I want to type text, and send it like showed in the following link
example of form to be filled out.
I am however unable to type anything in the field. I supposed at first it was an iframe, but splinter is unable to locate it. If i search by tags for the body, it says that:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
How might i go about fixing this?
if your element is not focused you have to first move to that element using selenium actions as following:
first move to using ;
driver.switch_to.frame(driver.find_element_by_css_selector("#inquiry-content_ifr"))
Then focus on element
yourElement= driver.find_element_by_css_selector(".nav")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.perform()
then do your interaction with that element after focus has been set to the element.
I have the following python code :
iframe = driver.find_element_by_name("iframe_name")
driver.switch_to_frame(iframe)
elem = driver.find_element_by_xpath("/html/body/iframe")
It is able to find the first iframe element and then switch to it however once it is in it and I try to access the second iframe element (by xpath since it does not have a name or id) I keep getting a "no such element error".
Can someone please help. I am trying to access the interior iframe so that I can get the src attribute within it.
Possible solutions.
Try using wait for an element by XPath ("/html/body/iframe"), as often times the driver will fail to wait till switching to the frame is completed.
Make sure that your XPath ("/html/body/iframe") is working. Also try identifying element using the tag name if there only one IFrame in the IFrame.
Hope that helps.