Usman_Ahmed
Usman_Ahmed

Reputation: 69

Selenium webdriver find_element_by_id() method in python throws error with phantomjs

web testcases on python based on selenium webdriver is having issues with using phantomjs. It is giving error on driver.find_element_by_id("username").

Relevant HTML:

<input class="form-control" name="username" id="username" type="text" placeholder="Username">

I heard it is time wait issue, I tried before and after the time.sleep(10) but still no progress:

time.sleep(10)
user = self.driver.find_element_by_id("username")
time.sleep(10)

can pleas anybody know about this like running cases with chromrdriver is working but with phantomjs it is not working even after accessing phantomjs.exe in testcases.

Upvotes: 0

Views: 360

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193198

As per the HTML you have provided in your comments and subsequently updated within the question, you can remove all the time.sleep() instances and replace them with WebDriverWait as follows:

  • CSS_SELECTOR:

    user = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control#username")))
    
  • XPATH:

    user = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control' and @id='username']")))
    

Upvotes: 1

Related Questions