2

I am trying to automate a process using Python and selenium webdriver. I am able to login successfully and navigating to the page where I want to post something but for some reason the xpath is not recognized by the system.

It gives the below error:

NoSuchElementException: Unable to locate element: //*[@id="widecol"]/div/form/p[1]/input

Here is my code:

from selenium import webdriver
mydriver = webdriver.Firefox()
mydriver.get("https://www.phishtank.com/")
mydriver.maximize_window()
mydriver.find_element_by_xpath('//*[@id="username"]').send_keys("myusername")
mydriver.find_element_by_xpath('//*[@id="password"]').send_keys("mypassword")
mydriver.find_element_by_xpath('//*[@id="header"]/div[2]/form/input[3]').click()
mydriver.find_element_by_xpath('//*[@id="nav"]/ul/li[2]/a').click()
mydriver.find_element_by_xpath('//*[@id="widecol"]/div/form/p[1]/input').send_keys("sample text testing to fill form")

This is my HTML code

<div id="widecol">
<div class="padded">
    <form method="POST">
    <h2>Add A Phish</h2>
    <ol>
        <li>Visit our <b><a href="what_is_phishing.php">What is phishing?</a></b> page to confirm that the suspected phish meets all of the criteria.</li>
        <li>Add a phish using the form below, or even better, submit a phish directly <a href="mailto:phish@phishtank.com">via email</a>.</li>
    </ol>
    <h3>Phish URL:</h3>
            <p>
            <input type="text" name="phish_url" style="width:90%;" value="" /><br />
            <span class="small">Copy and paste the URL of the phishing website.</span>
    </p>
    <h3>What is the organization referenced in the email?</h3>
            <p class="slim">
        <select name="phish_target">

which gives me the following error:

NoSuchElementException: Unable to locate element: //*[@id="widecol"]/div/form/p[1]/input

Here is the HTML code:

<input type="text" name="phish_url" style="width:90%;" value=""> outer HTML code

And this is my XPath:

//*[@id="widecol"]/div/form/p[1]/input - Xpath

Please let me know where to look, thank you.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Kunal
  • 25
  • 1
  • 6

4 Answers4

2

You need to induce WebDriverWait for the element to be clickable and you can use the following line of code :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='phish_url' and @type='text']"))).send_keys("sample text testing to fill form")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi thank you for your help. I tried ruining the above line of code but it still give the same error : NoSuchElementException: Unable to locate element: //*[@id='widecol']//input[@name='phish_url'] – Kunal Apr 19 '18 at 21:32
  • @Kunal I am sure you haven't tried out my code as my solution doesn't contains `@id='widecol'`. Try out exactly my code and let me know the status. – undetected Selenium Apr 19 '18 at 21:36
  • 1
    Perfect, it works. Thanks a lot. Sorry, I forgot to delete the older xpath line, it's a great help. – Kunal Apr 19 '18 at 21:45
1

Could you try the following instead?

//*[@id='widecol']//input[@name='phish_url']

Should work for that input. Just for trial/error sake, try a small wait before doing this.

Anand
  • 1,899
  • 1
  • 13
  • 23
  • Hi, thanks. Itried running this but it gives the same error: NoSuchElementException: Unable to locate element: //*[@id='widecol']//input[@name='phish_url'] – Kunal Apr 19 '18 at 21:25
1

The new Selenium version doesn't allow it.

Try the below code

from selenium.webdriver.common.by import By

And then Change the 'find_element_by_xpath statements' to

mydriver.find_element(By.XPATH,'//[@id="username"]').send_keys("myusername")

You can refer to the link to know more

  • You can also go to Chrome / Firefox and other browser and copy the `xpath` of an object and use it in Selenium as above. Hope that helps! https://stackoverflow.com/questions/3030487/is-there-a-way-to-get-the-xpath-in-google-chrome – Muneeb Ahmad Khurram Dec 23 '22 at 15:48
0

Without the full HTML, we can't tell if the XPath is correct or no. Can you please try and include more outer Html code?

You can try and wait after clicking on that last URL, maybe the element wasn't loaded yet. You can also check if the input is in an iframe, that might be causing the problem.

Ayoub_B
  • 702
  • 7
  • 19
  • Sure, thanks. I have added more Html code. The more HTML source code can also be found at https://www.phishtank.com/add_web_phish.php. – Kunal Apr 19 '18 at 20:21