简体   繁体   中英

Finding page element using selenium(Python)

Im trying to find the username path for the login page on https://www.textnow.com/login . I've tried finding it by x_path, ID, Name, class but my bot just cant find it. Does anyone have any possible solutions that I would be able to try out ?

Source Code:

"SUDO FUNCTION: OPEN A NEW TAB FOR TEXT NOW AND LOG IN"
driver.implicitly_wait(3)
driver.execute_script("window.open('http://www.textnow.com/login','new window')")

textNowEmail = driver.find_element_by_id('txt-username')# still have not found username textfield 
textNowEmail.send_keys(textNowUser)

#Set password code 
textNowPass = driver.find_element_by_id('txt-password')
textNowPass.send_keys('fill')

This is the message im getting:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="txt-username"]"}
  (Session info: chrome=78.0.3904.108)

You are opening a new window. Are you switching to it ? To make sure you are on the right window you can get page source by using "driver.get_source()" method and then evaluate the DOM.

Considering there are 2 window handles, you can switch to newly opened window using following :

required_window = driver.window_handles[1]
driver.switch_to_window(required_window)

Also try using "WebDriverWait" and "expected_conditions" to wait till required element is present by importing following:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

and then finding element using expected conditions:

WebDriverWait(driver,5).until(
         EC.presence_of_element_located((By.ID, "txt-username")))

When you are opening new window with execute_script your window handle is still in the original window. You need to switch window.

You can check all the windows available with driver.window_hanldes

For your case just use

driver.switch_to.window(driver.window_handles[1])

after opening new window

then proceed with rest of your code

To send a character sequence to the Email or Username and Password fields with in the website https://www.textnow.com/login you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') driver.get("https://www.textnow.com/login") WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.uikit-text-field__input#txt-username"))).send_keys("Xavier-Uriel-Espinal") driver.find_element_by_css_selector("input.uikit-text-field__input#txt-password").send_keys("Xavier-Uriel-Espinal")
  • Using XPATH :

     options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path=r'C:\\Utility\\BrowserDrivers\\chromedriver.exe') driver.get("https://www.textnow.com/login") WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='uikit-text-field__input' and @id='txt-username']"))).send_keys("Xavier-Uriel-Espinal") driver.find_element_by_xpath("//input[@class='uikit-text-field__input' and @id='txt-password']").send_keys("Xavier-Uriel-Espinal")
  • Note : You have to add the following imports :

     from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:

文本现在

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM