I had to locate the iframe ,my html source code looks like
<iframe border="0" scrolling="yes" src="problem_list.do?sysparm_query=u_service_ticket_triage_group.name!%3Dxbt%20tech%20support%5Eu_program.name%3DTX_STAAR%5Eu_reject_ticket!%3Dtrue" name="TribName" width="100%" allowtransparency="true" style="width: 100% !important; height: 800px !important;" frameborder="0" id="TribID"></iframe>
I tried using id and name to locate this iframe but i got error
"no such element: Unable to locate element:"
Following are once that i tried
iframe = driver.find_element_by_name("TribName")
driver.switch_to.frame(iframe)
iframe = driver.find_element_by_id("TribID")
driver.switch_to.frame(iframe)
Could you suggest on how I can select this iframe.
As per the HTML you have provided to switch to the desired frame you need to use WebDriverWait for the frame to be available and then switch using either of the methods :
Through Frame ID :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"TribID")))
Through Frame Name :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"TribName")))
Iframe might be generate dynamically so you might need to explicitly wait for its appearance:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("TribID"))
In case of nested frames, e.g.
<iframe id="OuterFrame">
<iframe id="TribID"></iframe>
</iframe>
you should switch sequentially to each frame starting from the high level ancestor:
driver.switch_to.frame("OuterFrame")
driver.switch_to.frame("TribID")
Related
I'm using selenium to automate some tasks with webdriver.
Turns out I can't find a div to click on, selenium just can't find it.
Does anyone have a suggestion?
HTML :
<div aria-controls="leftAdvPnl_body" aria-expanded="false" aria-haspopup="true" class="rich-stglpanel-header " id="leftAdvPnl_header" onclick="SimpleTogglePanelManager.toggleOnClient(event,'leftAdvPnl');" onkeypress="return keypressclickhandle(event);" role="link" tabindex="0"><div aria-hidden="true" class="rich-stglpanel-marker"><div class="rich-stglpnl-marker" aria-hidden="true" id="leftAdvPnl_switch_on" style="display: none">«</div><div class="rich-stglpnl-marker" aria-hidden="true" id="leftAdvPnl_switch_off">»</div></div><span id="leftAdvPnl_header_label">Pesquisar</span><span aria-hidden="true"> </span></div>
Code phyton:
while len(navegador.find_elements_by_xpath('//*[#id="leftAdvPnl_header"]')) < 1:
time.sleep(1)
print("Procurando formulário do processo")
link=navegador.find_element_by_xpath("//*[#id="leftAdvPnl_header"]")
link.click()
Thanks!
You have 2 nested double quotes, instead of "//*[#id="leftAdvPnl_header"]" use '//*[#id="leftAdvPnl_header"]'
You need to wait for an element to be clickable, so try:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="leftAdvPnl_header"]'))).click()
To click on the element with text as Pesquisar you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "div.rich-stglpanel-header#leftAdvPnl_header span#leftAdvPnl_header_label").click()
Using xpath:
driver.find_element(By.XPATH, "//span[text()='Pesquisar' and #id='leftAdvPnl_header_label']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.rich-stglpanel-header#leftAdvPnl_header span#leftAdvPnl_header_label"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Pesquisar' and #id='leftAdvPnl_header_label']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
xpath that you should check :
//div[#id='leftAdvPnl_header']
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
If we have 1/1 matching node, Please make sure that :
This div is not under an iframe.
This div is not under a shadow-root.
You should not be on new tab/windows launched by selenium.
Code :
try:
WebDriverWait(navegador, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='leftAdvPnl_header']"))).click()
print("Clicked on web element")
except:
pass
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Updated :
CSS are far more better locator than XPath, please try the below one :
div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']
Same way to check if we have 1/1 matching node or not.
To click, there are 4 ways in Selenium.
Code trial 1:
time.sleep(5)
driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']").click()
Code trial 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']"))).click()
Code trial 3:
time.sleep(5)
button = driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']")
ActionChains(driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
how to click a button in a webpage without class name or id,
this is the web code
<a href="https://example.com" style="text-decoration:none;line-height:100%;background:#5865f2;color:white;font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:15px;font-weight:normal;text-transform:none;margin:0px;" target="_blank">
Verify Email
</a>
I tried this code
driver.find_element_by_xpath('/html/body/div/div[2]/div/table/tbody/tr/td/div/table/tbody/tr[2]/td/table/tbody/tr/td/a').click()
but it show this in the log
Message: no such element: Unable to locate element:
and I tried this too
driver.find_element_by_link_text('Verify Email').click()
and it show this in the log
Message: no such element: Unable to locate element:
and I tried to add time.sleep(5) before it but the same problem
looks like there are spaces, so can you try with partial_link_text as well :-
driver.find_element_by_partial_link_text('Verify Email').click()
or with the below xpath :
//a[#href='https://example.com']
or
//a[contains(#href,'https://example.com')]
or
//a[contains(text(),'Verify Email')]
Update 1 :
There is an iframe involved, so driver focus needs to be switch to iframe first and then we can click on Verify Email, see below :-
Code :
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.gmailnator.com/sizetmp/messageid/#17b0678c05a9616e")
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "message-body")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Verify Email')]"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try this XPath locator:
//a[contains(text(),'Verify Email')]
Or this:
//a[#href='https://example.com']
So your code will be
driver.find_element_by_xpath("//a[contains(text(),'Verify Email')]").click()
or
driver.find_element_by_xpath("//a[#href='https://example.com']").click()
Also possibly you have to add a wait to let the element loaded.
In this case your code will be:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[contains(text(),'Verify Email')]"))).click()
or
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[#href='https://example.com']"))).click()
I am trying to automate pulling credit charges into an excel sheet; I have managed to get the login working. Once I enter the website, there's a button titled "Search". I cant seem to figure out how to have that button clicked.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time
chromedriver = "C:/Python_Ex/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
delay = 30
driver.get("https://global.americanexpress.com/activity/date-range?from=2020-05-01&to=2020-05-30")
driver.find_element_by_xpath('//*[#id="eliloUserID"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="eliloPassword"]').send_keys("removed")
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
time.sleep(10)
#print(driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button'))
search_button = driver.find_elements_by_xpath('//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button')
search_button.click()
html tag is as follows
<button class="btn btn-fluid" tabindex="0" type="button"> <span>Search</span></button>
Xpath as follows
//*[#id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button
Any help is appreciated.
To click() on the <button> with text as Search using Selenium you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-fluid[type='button']>span"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']/span[text()='Search']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
After login submited, try to add the below code:
...
...
driver.find_element_by_xpath('//*[#id="loginSubmit"]').click()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-fluid']//span[text()='Search']"))).click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I have the following HTML markup:
<div data-request-type="person" class="_entry _line _e _selected" id="c1055e27-0cfe-4f93-8bea-28a3421d842e" data-rolename="Member" data-isoptional="true">
<div class="_subindicator _gray"> </div>
<div class="_removePers"> </div>
<div class="_voluntaryPers"> </div>
<div class="_text">Member</div>
</div>
I have to click on the first <div> using .click().
But now I don't know how to find this with selenium. I already tried it with XPATH, but I have several elements with different IDs on this page. And the IDs are always regenerated. This does not work.
Does anyone have an idea?
I tried it with a lot of solutions...but nothing works.
My last one - to take a inner div with class _text
getAllMembers = browser.find_element_by_css_selector('td._text').get_attribute('innerHTML')
Please help - how can I do this with Selenium? :-)
Try below xpath :
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='_entry _line _e _selected'][#data-rolename='Member']"))).click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
To select and click on a element by its attribute, you can use:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.get("https://site.tld")
xpath = "//div[#data-rolename='Member']"
el = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
el.click()
I'm trying to check the checkbox using Selenium. This is the element of the checkbox.
<div class="ams-item-text ng-binding" ng-bind-html="amssh.create_label(item)" ng-click="toggle_check_node(item)" role="button" tabindex="0">all contract signed</div>
I'm directly copying the x-path of this element and wrote the below code:
browser.find_element_by_xpath('//*[#id="advancedcontents"]/div/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div/div[2]/div[3]/div[2]').click()
But it won't never click the check box that I wanted, but click the check box way below, which is with this:
<div class="ams-item-text ng-binding" ng-bind-html="amssh.create_label(item)" ng-click="toggle_check_node(item)" role="button" tabindex="0">future</div>
What could be the issues? I try the checkbox element, or the text element (also clickable) but both doesn't work.
To click on dynamic element induce WebDriverWait() and visibility_of_element_located() and following xpath option.
WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='ams-item-text ng-binding' and text()='all contract signed']"))).click()
You need to import following libraries.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Try below code:
wait = WebDriverWait(browser, 20)
wait.until(EC.element_to_be_clickable(By.XPATH, "//div[contains(.,'all contract signed')]")).click()
Note : Add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
"ams-item-text ng-binding"browser.find_element_by_xpath('//*[#id="advancedcontents"]/div/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div/div[2]/div[3]/div[2]').click()