1
votes

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:[email protected]">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.

3

3 Answers

1
votes

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")
0
votes

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.

0
votes

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.