Selenium finding hidden element - python

I'm having difficulty accessing a dropdown menu in www.meridiancu.ca. It's the one under "Select Banking Type" on the right side of the homepage. Once I run my code.
from selenium import webdriver
from selenium.webdriver.common import action_chains, keys
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.meridiancu.ca")
bank_type = driver.find_element_by_id('SelectAccount')
bank_type.click()
I get the following output, and I'm not sure how I can find this "hidden" element.
DevTools listening on ws://127.0.0.1:12015/devtools/browser/6f5fba77-4c41-49b9-93a3-64a8363cd35b
Traceback (most recent call last):
File "C:\Users\Imad\Documents\Programming\Python\dropdown select.py", line 14, in <module>
bank_type.click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=62.0.3202.94)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86_64)
Any suggestions or solutions would be greatly appreciated.

The following code for working with select tag:
from selenium.webdriver.support import ui
ui.Select(driver.find_element_by_css_selector(".sign-in-panel.sign-in-banner #SelectAccount")).select_by_visible_text("Small Business Banking")
Result: The "Small Business Banking" option should be selected from the dropdown.
Hope it helps you!

The issue you're encountering is that your selector is finding 2 elements that match. The first element that matches is hidden. The hidden element is displayed when the page is viewed by a mobile browser.
I did some investigating, and found that the following CSS selector will find the element you are looking for.
bank_type = find_element_by_css_selector('.show-for-large select#SelectAccount.banking-target')

You can also do the iteration over all options in the drop down, using the following code snippet.
el = driver.find_element_by_id('SelectAccount')
for option in el.find_elements_by_tag_name('option'):
if option.text == 'Personal Banking':
option.click() # select() in earlier versions of webdriver
break

Related

Python Selenium cannot find an element on a page by xpath

I am using python and selenium to test some things with fantasy football. Here is my code so far (I just started).
import time
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\202300Fontenot\\Desktop\\3\\chromedriver.exe')
driver.get('http://games.espn.com/ffl/signin?redir=http%3A%2F%2Fgames.espn.com%2Fffl%2Fclubhouse%3FseasonId%3D2018%26leagueId%3D49607%26teamId%3D4');
driver.implicitly_wait(10)
time.sleep(10)
search_box = driver.find_element_by_xpath('//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input')
search_box.send_keys('email#icloud.com')
search_box.submit()
time.sleep(5)
driver.quit()
This just tries to enter an email address into the box. I am getting this error every time:
Traceback (most recent call last):
File "C:\Users\202300Fontenot\Desktop\3\ESPN.py", line 8, in <module>
search_box = driver.find_element_by_xpath('//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input')
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 393, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input"}
(Session info: chrome=69.0.3497.92)
(Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.16299 x86_64)
Thanks for your help.
Your problem is that the input you are trying to select exists inside of an iframe. You must first tell the driver to switch to the iframe and then execute the xpath selection.
driver.switch_to.frame("disneyid-iframe")
driver.find_element_by_xpath("//input[#type='email']").send_keys('email#icloud.com')
First of all you need to switch to your iframe using its name:
disneyid-iframe
Also if XPATH is not the only choice, you may use the CSS Selector:
div.field-username-email input[type='email']
This will get the driver to find & fill your field.
Using XPath in python You need to provide the string for
driver.find_element_by_xpath
Example Code below
("//*[#id=\"did-ui-view\"]/div/section/section/form/section/div[1]/div/label/span[2]/input")
Please refer below document regarding String
String Gude first Para

Python Selenium Cannot find ID in website

Why is it whenever I try to find "uxStartDateDecisionTextBox" it says it cannot find the ID. On the website they have another ID called "BodyPlaceHolder_uxTextSearchKeywords" and whenever I try to find that ID it works perfectly fine. What is the issue? I tried googling it but nothing helped, so I am resorting to asking on here.
Code:
from selenium import webdriver
url = "http://www.fareham.gov.uk/casetrackerplanning/applicationsearch.aspx"
driver = webdriver.Chrome(executable_path=r"C:\Users\Goten\Desktop\chromedriver.exe")
driver.get(url)
driver.find_element_by_id("lnkAllowCookies").click()
driver.find_element_by_id("BodyPlaceHolder_uxLinkButtonShowAdvancedSearch").click()
driver.find_element_by_id("uxStartDateDecisionTextBox").click()
driver.find_element_by_id("uxStartDateDecisionTextBox").clear()
driver.find_element_by_id("uxStartDateDecisionTextBox").send_keys("01/08/2018")
driver.find_element_by_id("uxStopDateDecisionTextBox").click()
driver.find_element_by_id("uxStopDateDecisionTextBox").clear()
driver.find_element_by_id("uxStopDateDecisionTextBox").send_keys("30/08/2018")
driver.find_element_by_id("BodyPlaceHolder_uxButtonSearch").click()
Error:
Traceback (most recent call last):
File "C:\Users\Goten\Desktop\sel.py", line 11, in <module>
driver.find_element_by_id("uxStartDateDecisionTextBox").click()
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 359, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"uxStartDateDecisionTextBox"}
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
Before one clicks on the 'Advanced Search' link, the entire Advanced Search Form is not at all present in the DOM. After clicking on the link, the entire form is loaded in the DOM at runtime. So I can't think of anything other than this being a synchronization issue.
Try adding a step before
"driver.find_element_by_id("uxStartDateDecisionTextBox").click()" that pauses the execution for a second or so. If after that, Selenium is able to locate the element, you can be pretty sure that this is a synchronization problem. Then you can opt for some kind of explicit waits to improve your execution speed.

Copy text from a table cell (td) with Selenium/Python

Hi I'm very new to Selenium and would really appreciate help.
I need to learn how to copy text, particularly from a table from a specific website: https://aca.tampagov.net/citizenaccess/Default.aspx#
(Go to url --> Search --> Building Permits --> click search button).
You can finds a table at the bottom of the page.
The first row in column "Record Type" has text "Commercial Utility Application". If I want to copy the text what I thought I should do is get the xpath which is
//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType"]
So I would have something like
driver.find_element_by_xpath('//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
But this gives me and error. What am I doing wrong here? And how would I be able to copy texts from such tables?
EDIT
So here's what I get:
Traceback (most recent call last):
File "<ipython-input-81-4e51d9229198>", line 1, in <module>
driver.find_element_by_xpath('//* [#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 387, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 957, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]"}
(Session info: chrome=67.0.3396.99) (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
Your table is in iframe, so you have to switch to it before you can interact with this table. This code snippet will help you to do it:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//*[#id='ACAFrame']")))
# do your stuff
text = driver.find_element_by_xpath('//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]').text()
print(text)
driver.switch_to.default_content() # switch back to default content
PS: since in the page there is only one table, you can simplify your xPath from:
//*[#id="ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList"]/tbody/tr[3]/td[4]
to:
//tbody/tr[3]/td[4]
To retrieve text Commercial Utility Application, you should try -
driver.findElement(By.id("ctl00_PlaceHolderMain_dgvPermitList_gdvPermitList_ctl02_lblType")).getText();

Not able to click list item in a dropdown menu in selenium python

I want to click the excel button under export drop down button.But even I am able to access the menu.When I am trying to click on the excel option under it,a message is being displayed that unable to locate element.
Here is the exact error message:
Traceback (most recent call last):
File "C:/Users/shishir sinha/PycharmProjects/australia/australia.py", line 33, in
driver.find_element_by_xpath(".//[#id='ui-menu-0-1']").click()
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 309, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 787, in find_element
'value': value})['value']
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute
self.error_handler.check_response(response)
File "C:\Users\shishir sinha\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//[#id='ui-menu-0-1']"}
(Session info: chrome=58.0.3029.110)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.14393 x86_64)
Here is the link to website:
https://stats.oecd.org/Index.aspx?DataSetCode=STAN08BIS
Here is the code:
_author_ = 'shishir'
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Chrome("C:\\Users\\shishir sinha\\AppData\\Local\\Programs\\Python\\Python36\\selenium\\webdriver\\chromedriver_win32\\chromedriver.exe")
driver.get("https://stats.oecd.org/Index.aspx?DataSetCode=STAN08BIS")
driver.find_element_by_xpath(".//*[#id='PDim_COU']").click()
driver.find_element_by_xpath(".//*[#id='PDim_COU']/option[1]").click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH, ".//*[#id='PDim_VAR']")))
driver.find_element_by_xpath(".//*[#id='PDim_VAR']").click()
action = webdriver.ActionChains(driver)
driver.find_element_by_xpath(".//*[#id='PDim_VAR']/option[3]").click()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, ".//*[#id='menubar-export']/a/span[1]/span[2]")))
driver.find_element_by_xpath(".//*[#id='menubar-export']/a/span[1]/span[2]").click()
driver.find_element_by_xpath(".//*[#id='menubar-export']/a/span[1]/span[2]").click()
action.move_to_element(driver.find_element_by_xpath(".//*[#id='menubar-export']/a/span[1]/span[2]"))
driver.find_element_by_xpath(".//*[#id='ui-menu-0-1']").click()
Most likely the actual <select> element is not visible on the page and the select you see on the page is a pseudo-select element made for example of <div> or <li> elements.
This is now becoming more common, given that there are limited options to style native selects. As a result many frameworks 'overlay' them with better looking components and delegate the logic to the hidden underlying select via javascript.
The solution will be to find the actual visible elements and click them instead of the <select> or use driver.execute_script(...) to drive the widgets via javascript.

Upload file with Selenium Webdriver Python

I have tried the method from this page:
Upload file with Selenium in Python
Code:
file_button = browser.find_element_by_id('fileUploadProxy')
file_button.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
But I get the following error:
Traceback (most recent call last):
File "test.py", line 110, in <module>
upload_students_results('Surname, Name')
File "test.py", line 91, in upload_students_results
file_button.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 349, in send_keys
'value': keys_to_typing(value)})
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 249, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 193, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
(Session info: chrome=58.0.3029.96)
(Driver info: chromedriver=2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b),platform=Mac OS X 10.12.4 x86_64)
The problem is - you are sending keys to the div element which is not "interactable", does not accept the keys - hence the "cannot focus element" error.
The idea behind the solution you've linked is to send keys to the input element with type="file" that is responsible for the file upload. Find this element in your HTML and send keys to it.
Note that this element could be invisible. In this case, you should first make it visible for the send_keys() to work.
Update:
Okay, now we at least know which element is our desired one:
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
Since you have troubles locating this element, either try waiting for it:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
file_upload = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "fileToUpload2"))
)
file_upload.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')
Or/and, check if this element is inside an iframe - if it is, you would need to switch into the context of the iframe and only then perform the element search.
I had the same problem when I inserted the file path as a string. This is functional:file_input.send_keys(os.path.abspath("path/to/the/file.xyz"))

Categories

Resources