https://www.gmx.net
I want to click on Zustimmen und weiter.
driver.get('https://www.gmx.net')
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '/html/body/iframe')))
driver.find_element_by_xpath('//*[#id="save-all-conditionally"]').click()
The code above is not working for me.
Element you are trying to access is located inside nested iframe, one iframe insde another iframe.
You should switch to the first iframe, then to the inner iframe and then to click the "Get cookies" button.
This should work:
wait = WebDriverWait(driver, 20)
wait.until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#class="permission-core-iframe"]')))
wait.until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(#src,'plus')]")))
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="save-all-conditionally"]'))).click()
When you finished working inside that iframe you will have to switch to the default context with
driver.switch_to.default_content()
Related
I want to enter the "Symbol" name and select tab "NSE" from the filter. Once that is done, click on table view button using Selenium. But I cannot find any of these nodes using Debugger.
(https://i.stack.imgur.com/uOrQc.png)
The original class "chart-frame" is found.
driver.find_element(By.ID, "chart-iframe")
I get "No such element" for any of the below commands. I cannot find any information on this CQ tags online.
driver.find_element(By.CLASS_NAME, "ciq-search")
driver.find_element(By.CSS_SELECTOR, "ciq-search")
driver.find_element(By.CSS_SELECTOR, "ciq-DT.tableview-ui")
driver.find_elements(By.XPATH, '//button')
Firstly, you need to switch into that iframe to scrape whatever you wanna scrape. You can use following code snippet for it:
iframe = driver.find_element(By.XPATH, "//iframe[#name='TheIframeName']")
driver.switch_to.frame(iframe)
You can use driver.switch_to.default_content() to switch back to default content.
I'm trying to locate a element, but I can't click on it. The id is ("save-all-conditionally"), but it's not working if I click on it. I tried css.selector, xpath and all other things, but nothing is working!
There are 2 frames, frame inside frame.
You would need to switch to parent frame then child frame.
here is the working code :
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.gmx.net/consent-management/")
driver.implicitly_wait(10)
firstFrame = driver.find_element_by_xpath("//iframe[#class='permission-core-iframe']")
driver.switch_to.frame(firstFrame)
driver.switch_to.frame(0)
driver.find_element_by_xpath("//button[#id='save-all-conditionally']").click()
Try this code, see if it works:
ele = driver.find_element_by_xpath("//*[#id='save-all-conditionally']")
ele.click()
Not tested yet:
iframe = driver.find_elements_by_tag_name('iframe')
if len(iframe) == 1:
driver.switch_to.frame(iframe[0])
button = driver.find_element_by_id('save-all-conditionally')
if button:
button.click()
Since the iframe has no id get it by tag name, it should be one iframe use switch_to.frame to switch to the iframe content and get the button to click on it
I'm using Selenium to click the "Become A Member" button from this link: https://www2.hm.com/en_us/register.
Here is the HTML of the button: https://i.stack.imgur.com/Pjeu3.png
I have exhausted all other answers on this site: I have tried to find this element using XPath, CSS Selector, waited for element to be clickable, visible, etc. all but to no avail.
Here is my current code that accepts all the cookies (since I thought that was the problem) and then tries to click on the "Become a Member" button
try:
# Accepts cookies
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='onetrust-accept-btn-handler']"))).click()
# Clicks the register button
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-testid='submitButton']"))).click()
except:
print("Driver waited too long!")
driver.quit()
Does anyone know what I can do to fix this issue? Thank you!
to click on Become A Member button you are using input[data-testid='submitButton'] which is almost correct but it's not input tag, it's a button.
See the HTML here :
<button class="CTA-module--action__3hGPH CTA-module--medium__dV8ar CTA-module--primary__3hPd- CTA-module--fullWidth__1GZ-5 RegisterForm--submit__2Enwx" data-testid="submitButton" type="submit"><span>BECOME A MEMBER</span></button>
so changing input[data-testid='submitButton'] to button[data-testid='submitButton'] did the trick and it worked.
Sample code : -
try:
# Accepts cookies
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='onetrust-accept-btn-handler']"))).click()
# Clicks the register button
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='submitButton']"))).click()
except:
print("Driver waited too long!")
driver.quit()
How would I switch to this iframe in selenium knowing only
<iframe name="Dialogue Window">
You can use an XPath to locate the <iframe>:
iframe = driver.find_element_by_xpath("//iframe[#name='Dialogue Window']")
Then switch_to the <iframe>:
driver.switch_to.frame(iframe)
Here's how to switch back to the default content (out of the <iframe>):
driver.switch_to.default_content()
As per the HTML of the <iframe> element, it has the name attribute set as Dialogue Window. So to switch within the <iframe> you need to use the switch_to() method and you can use either of the following approaches:
Using the name attribute of the <iframe> node as follows:
# driver.switch_to.frame(‘frame_name’)
driver.switch_to.frame("Dialogue Window")
Using the <iframe> WebElement identified through name attribute as follows:
driver.switch_to.frame(driver.find_element_by_name('Dialogue Window'))
Using the <iframe> WebElement identified through css-selectors as follows:
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[name='Dialogue Window']"))
Using the <iframe> WebElement identified through xpath as follows:
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[#name='Dialogue Window']"))
Ideally, you should induce WebDriverWait inconjunction with expected_conditions as frame_to_be_available_and_switch_to_it() for the desired frame as follows:
Using NAME:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"Dialogue Window")))
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name='Dialogue Window']")))
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='Dialogue Window']")))
Switching back from frame
To switch back to the Parent Frame you can use the following line of code:
driver.switch_to.parent_frame()
To switch back to the Top Level Browsing Context / Top Window you can use the following line of code:
driver.switch_to.default_content()
tl; dr
Ways to deal with #document under iframe
I have resolved this issue in Python-Selenium. Please use the below code:
srtHandle = driver.window_handles
driver.switch_to_window(srtHandle[0])
Then switch to the frame in which the element is located.
There is a form on this site and I am trying to get every option from it and click a search button with this code:
from selenium import webdriver
driver = webdriver.PhantomJS('c:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
driver.get('http://www.cobar.org/Find-A-Lawyer')
driver.implicitly_wait(20)
options = driver.find_element_by_xpath('//select[#id="FAL_FOP_field"]')
for option in options.find_elements_by_tag_name('option'):
if option.text != 'ALL':
option.click()
#click search button
driver.find_element_by_xpath('//button[#class="btn btn-primary btn-main"]').click()
lawyer = driver.find_element_by_xpath('//table[#id="myTable"]/tbody/tr/td[0]')
print(lawyer)
However I am getting:
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":
"Unable to find element with xpath '//select[#id=\"FAL_FOP_field\"]'",
"request":{"headers":{"Accept":"application/json","Accept-Encoding":
"identity","Connection":"close","Content-Length":"115","Content-Type":
"application/json;charset=UTF-8","Host":"127.0.0.1:52809","User-Agent":"Python-urllib/2.7"}
,"httpVersion":"1.1","method":"POST","post":
"{\"using\": \"xpath\", \"sessionId\": \"e03be070-e353-11e6-83b5-5f7f74696cce\","
" \"value\": \"//select[#id=\\\"FAL_FOP_field\\\"]\"}","url":"/element",
"urlParsed":{"anchor":"","query":"","file":"element","directory":"/",
"path":"/element","relative":"/element","port":"","host":"","password":"","user":"",
"userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},
"chunks":["element"]},"urlOriginal":"/session/e03be070-e353-11e6-83b5-5f7f74696cce/element"}}
what should I do?
Error is because fields you are locating are inside iFrame.
So, first you need to switch to iframe and then locate your elements.
still, if didn't work, add time delay.
To locate iFrame :
WebElement iframelocator = driver.findElement(By.xpath("//iframe[#id='dnn_ctr2047_IFrame_htmIFrame']"));
Then Switch to iFrame
driver.switchTo().frame(iframelocator);
Add above two steps in your code before locating elements.
Note : Above written code is in java.