Newbbit
Newbbit

Reputation: 33

python selenium element not visible

import webbrowser
from selenium import webdriver

browser = webdriver.Chrome()
browser.get('https://www.suntrust.com/')
browser.implicitly_wait(10)
elem = browser.find_element_by_xpath('//*[@id="sign-on-3A69E29D-79E0-403E-
                                     9352-5261239ADD89-user"]')
elem.send_keys('your-username')

I'm having two problems:

1) The window doesn't open up in full screen, meaning the username field isn't physically visible. How do I open the url in a new tab instead of a new window.

2) Other posts suggest that the element is faked by JavaScript so that webdriver can't see it.

I've tried find_element_by in all the other locators.

Upvotes: 2

Views: 1873

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193098

Here is the Answer to your Question:

The xpath you have constructed is not unique. The xpath matches exactly to 2 elements on the HTML DOM. So Selenium was trying to send_keys on the first matching element which was invisible. Hence the error element not visible. The xpath used in the following code block identifies the User ID field uniquely and sends the text:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
browser = webdriver.Chrome(chrome_options=options, executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
browser.get('https://www.suntrust.com/')
browser.implicitly_wait(15)
elem = browser.find_element_by_xpath('//section[@role="main"]//input[@id="sign-on-3A69E29D-79E0-403E-9352-5261239ADD89-user"]')
elem.send_keys('your-username')

Let me know if this Answers your Question.

Upvotes: 0

demouser123
demouser123

Reputation: 4264

Your question should be answered by a simple line of code that you need to include

browser.maximize_window()

would maximise your window. Another option is to set a specific window size like

driver.set_window_size(1280, 1024)

You can use both to achieve the browser being open to a maximum size.

Another point that I would make is that, if you're a beginner, try using more of CSS Selectors instead of the Xpath. They are much faster than Xpath's. Please see a detailed post on SQA about what makes a good locator.

For your case, the CSS Selector for the sign in field would be

driver.find_element_by_css_selector('input#sign-on-3A69E29D-79E0-403E-9352-5261239ADD89-user')

For password, it would be

driver.find_element_by_css_selector('input#sign-on-3A69E29D-79E0-403E-9352-5261239ADD89-password')

For Sign On button it would be

driver.find_element_by_css_selector('button.suntrust-login-button')

Please read more about CSS Selectors and try using them more often in your code.

Upvotes: 1

thebadguy
thebadguy

Reputation: 2140

If you use absolute xpath then you can send the text in textbox.

Below code will do that

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


browser = webdriver.Chrome()
browser.maximize_window() # to open full size window
browser.get('https://www.suntrust.com/')

# browser.implicitly_wait(10)
WebDriverWait(browser, 10).until(
    EC.presence_of_element_located((By.XPATH, '//*[@id="sign-on-3A69E29D-79E0-403E-9352-5261239ADD89-user"]')))

elem = browser.find_element_by_xpath("//div[@id='suntrust-login-form-herosignon']/div[2]/form/div[1]/input[1]")
elem.send_keys('your-username')
elem1 = browser.find_element_by_xpath("//div[@id='suntrust-login-form-herosignon']/div[2]/form/div[2]/input[1]")
elem1.send_keys('your-password')

Upvotes: 0

Related Questions