0

I am trying to access an element in Selenium, but none of the typical methods are working. So far I have tried using every variation of 'find_elements_by_x' with no success. I also spent about a day looking at various forums, but nothing seems to be working. I recently used Selenium in another successful project, but the same structure is not working for this particular website. Here is a snippet of the HTML containing the elements I am trying to access:

<input type="text" name="username" id="username" placeholder="Username / 
Email" autocapitalize="off" autocorrect="off" required="" ng-
model="credentials.username" class="ng-pristine ng-invalid ng-invalid-
required">

As is probably obvious, this is a username input for logging in. Below are a few lines I have tried so far that have not worked.

from selenium import webdriver


driver = webdriver.Chrome("chromedriver.exe filepath")
driver.get('url')

username = driver.find_element_by_xpath('//input[@id="username"]')

That XPath navigates to the element in question if I search for it using the console in Chrome.

I also tried:

username = driver.find_element_by_name('username')

which also did not work.

I am pretty new to Selenium, and I have no experience at all with HTML, so I don't know if there can be complications in the HTML that that must be taken into account when looking for elements through Selenium. Any help at all is appreciated. This is also my first time posting here, so I hope I did not violate any rules.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • Can you send us the URL so we can try it out? Most likely problem is that the input box is not loading immediately, in which case you can put a few seconds' delay on your request. – NFB Jan 07 '18 at 20:29
  • make sure you are getting the same html page otherwise selenium could not find it – johnII Jan 07 '18 at 20:29
  • I had a much more convoluted code containing delays. Looking again, I think it's because chrome itself is sending a request, not the webpage. It's asking if it can store files locally. Also, the html is http://www.runescape.com/companion/comapp.ws –  Jan 07 '18 at 20:33
  • Possible duplicate of [selecting an iframe using python selenium](https://stackoverflow.com/questions/7534622/selecting-an-iframe-using-python-selenium) – JeffC Jan 08 '18 at 04:12
  • The problem is that the elements are inside an IFRAME. You have to switch into the IFRAME context before you can access the elements inside the IFRAME. See the link above (or the Selenium docs) on how to do this. – JeffC Jan 08 '18 at 04:13

3 Answers3

-1

Looks like the input box is taking a few seconds to load. Try putting a delay before your request:

driver.implicitly_wait(3)

If that doesn't work, increase the number in parentheses a second at a time. If that solves the problem, you could instead do an explicit wait.

NFB
  • 642
  • 8
  • 26
  • It still throws an error claiming it cannot locate the element, even after they have visually been present for several seconds. The issue I believe is that a permission box is being sent by the browser requesting permission to save files locally. I have tried manually clearing it while the wait is still in effect, but that does not fix the problem either. I have also tried using 'sleep()' from the time module to no avail. –  Jan 07 '18 at 20:54
-1

Try the following code:

from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


options = Options()
options.add_argument("unlimited-storage")
driver = webdriver.Chrome(chrome_options=options)

driver.get("http://www.runescape.com/companion/comapp.ws")

iframe = ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.TAG_NAME, "iframe")))

# Switch to the frame.
driver.switch_to.frame(iframe)

username = ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "username")))

print(username)

# Switch to the main content.
driver.switch_to.default_content()

driver.quit()

Hope it helps you!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • Still isn't working. I think I am going to repost this question with more information, including the entire html and a screenshot of a promt that is coming up that I believe is the issue. Thank you (and everyone else). –  Jan 07 '18 at 22:11
  • @Brandon, I have updated my answer. This is working code. Take a look. Thanks. – Ratmir Asanov Jan 08 '18 at 08:51
  • @Brandon, how is going? – Ratmir Asanov Jan 09 '18 at 17:47
  • This answer is it. I didn't have a chance to test it. I was afraid it might be more difficult to navigate to it than I thought, and that was the case. I expect this will help with future projects as well. Much appreciated. –  Jan 10 '18 at 03:03
-1

As per the HTML you have shared the WebElement is a Angular element so you have to induce WebDriverWait for the element as follows :

  • CSS_SELECTOR :

    username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-pristine.ng-invalid.ng-invalid-required#username")))
    
  • XPATH :

    username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ng-pristine ng-invalid ng-invalid-required' and @id='username']")))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352