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
Related
I have 4 years of experience in Selenium, but I faced this kind of problem firstly.
I think only python selenium masters can solve this issue.
I used this function to get an Angular element, but it didn't work.
def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
return shadow_root
Chrome / chromedriver 96 and newer changed how interacting with Shadow DOM works. In order to interact with a Shadow DOM element with Selenium Python, you'll need to use a minimum of selenium version 4.1.0 or newer, and the method call you'll need to use is: element.shadow_root.
Additionally, there's a Selenium Python framework called SeleniumBase that can help simplify interactions with Shadow DOM elements. Here's an example test of that: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/shadow_root_test.py, which uses the SeleniumBase-specific ::shadow selector to pierce through an element that contains one or more shadow root segments.
How can I access embedded part from web page and contents using selenium and python
<embed src="RainPastDailyMonth.php" width="100%" height="100%">
Embedded part has input button elements which I need to access using selenium but getting
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"/html/body/center/b/input"}
for below code
driver.find_element(By.XPATH, '/html/body/center/b/input')
where '/html/body/center/b/input' is XPATH for input button
I am able to access the elements in an <embed> block by switching frame to <embed> element.
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'body > embed'))
Here, 'body > embed' is selector for <embed> element.
You cannot.
The <embed> element creates an embedded panel in which a third-party
application can run. In other words: it is outside of the DOM. Selenium is able to operate only on items in the browser's DOM.
You will need to access this third-party application using some other means. AutoIT or Sikuli are popular options.
I am using Selenium WebDriver to do automated testing of a website. I have been successful in clicking through numerous menus and links to a point.
At one point the website I am working with generates links that look like this:
<U onclick="HourglassSubmitItem(document.all('PageName').value, '00000001Responsibility Code')">Responsibility Code</U>
I am trying to use the .click functionality of the webdriver to click this link with no success.
Using this:
page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
successfully finds the U tag above. but when I add .click() to the end of this xpath, the click is not performed. But it also does not generate an error. So, my question is can Selenium be used to simulate clicks on an HTML tag that is NOT an anchor () tag? If so, how?
I will also say that I do not have control over the page I am working with, so changing the to is not possible.
I would appreciate any guidance the Community could provide.
Thank You for you help,
Chris
Sometimes using JavaScript could solve the "clicking" issue:
element = page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
page.execute_script('arguments[0].click();', element)
You can prefer JavaScript in this case.
WebElment element = page.find_element_by_xpath("//u[contains(text(),'Responsibility Code')]")
JavaScriptExecutor executor = (JavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", element);
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()
I am trying to click this radio button using selenium and python.
<input type="radio" name="tweet_target_1" value="website" class="tweet-website-button radio-selection-validate serialize-me newline-before field-order-15">
I have
website = driver.find_element(name="tweet_target_1")
website.click()
but it's not allowing me to click it. How can I click using a combo of name, value or class, value etc.?
Is there a good source of info about how to use selenium? Because most of what I've found is on java and I'm using python.
EDIT: using XPATH
I tried
website = driver.find_elements(By.XPATH, "//form[#id='dmca_form' and #class='twitter-form custom-form']/div[20][#class='list-container']/div[1][#class='list-item']/div[7][#class='clearfix inf-tweet init-hide']/div[#class='input']/ul[#class='options']/li[2]/label/input[#class='tweet-website-button radio-selection-validate serialize-me newline-before field-order-15']/")
website.click()
I keep getting
AttributeError: 'list' object has no attribute 'click'
I know this comes a little too late perhaps, but I joined just recently.
Tip: Use, Firebug and with it Firepath. Locate the radio button and find out the xpath for the element in question.
website = driver.find_element_by_xpath(".//**")
website.click()
OR
website = driver.find_element_by_xpath(".//**").click()
This should work all the time you try. Also, just using from selenium import webdriver
should make the click() function work correctly.
I'm not sure where you found the documentation that said you could call find_element like that, but you should either be doing driver.find_element_by_name("tweet_target_1") or driver.find_element(By.NAME, "tweet_target_1") (having first imported By of course). Also, Selenium Java code is pretty easily convertible to Python code; it follows a few pretty simple transformation rules, and if you still have questions, all the code for the library itself will also be on your machine to look at.