Finding an element using "find_element_by_xpath" - python-3.x

I am trying to find an element using "find_element_by_xpath" but getting error which says "Unable to locate element".
I have also tried giving implicit wait but still not able to locate the element.
The HTML file looks like:
<div class="actions">
<input type="submit" value="Save" data-action="save">
</div>
<input type="hidden" name=actionId>
I had tried below line of code:
driver.implicitly_wait(10)
driver.find_element_by_xpath("//input[#type='submit']")
driver.find_element_by_xpath("//class[#name='
actions']/input[#type='submit']"
driver.find_element_by_xpath("//form[#id='form-
submit']/input[#type='submit']")
All the above tries show the error "No such element: Unable to locate element"
I had tried "execute_script" for handling hidden element,
element = driver.find_element_by_xpath("//input[#type='submit']")
driver.execute_script("arguments[0].click();", element)
This is not giving any error but I am not sure if it is clicking on the correct button as a new webpage is opening with a message "The page you are trying could not be reached".

Your xpath seems to be fine, please check if the element is located inside an iframe and if yes, then first switch to the iframe and then you can operate on the element.
To switch to the iframe you can use: driver.switch_to_frame(driver.find_element_by_tag_name('iframe')) and then you can operate on the element and for switching back to the default content, you can use driver.switch_to_default_content()

Related

Unable to find element using python3 selenium

Im trying to fill a textfield on a website. I've tried to find the element using the ID and xpath. Both return and error element not found. I can't seem to figure out why I can't find the element. The html can be found here https://pastebin.com/jTXR6uwL. The part I'm trying to retrieve is
<input value="" data-error="Name is required" id="first_name" type="text" name="first_name"
class="form_item is_required" placeholder="First Name">
I've tried using the following code but they all return an error. I got the xpath using google chrome dev tools.
first_text = driver.find_element_by_id('first_name')
first_text = driver.find_element_by_xpath('//*[#id="first_name"]')
first_text =
driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[5]/div[2]/form/div[3]/span/input[1]')
All 3 return
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
Can someone explain why I'm unable to find this element?

Selecting a Value for a DojoComboBox using Selenium Python

I'm trying to build a data scraper with Selenium Python that searches a webpage. The search page contains a dojoComboBox that allows you to select a name from a dropdown list.
Here is the HTML for the dojoComboBox:
<span _="whitespace and CR's between tags adds in FF" class="dojoComboBoxOuter dj_khtml dj_safari dropSel" style="null">
<input style="display:none" tabindex="-1" name="txtCaseName" value="" dojoattachpoint="comboBoxValue">
<input style="display:none" tabindex="-1" name="txtCaseName_selected" value="" dojoattachpoint="comboBoxSelectionValue">
<input type="text" autocomplete="off" class="dojoComboBox dropSel" dojoattachevent="key:_handleKeyEvents; keyUp: onKeyUp; compositionEnd; onResize;" dojoattachpoint="textInputNode" style="null">
<img hspace="0" vspace="0" class="dojoComboBox dropSel" dojoattachpoint="downArrowNode" dojoattachevent="onMouseUp: handleArrowClick; onResize;" src="dojo/src/widget/templates/images/combo_box_arrow.png" style="width: 13px; height: 13px;">
</span>
I can get the DojoComboBox scroll menu to open with the following code:
dropdown = browser.find_element_by_css_selector("img.dojoComboBox")
dropdown.click()
However, I can't seem to find a way to actually select one of the values.
Update:
The HTML I posted above is from the page I pull up when I inspect elements (the HTML under the 'Elements' tab of Chrome DevTools). I've been looking at some other files that I found in the 'Source' tab, and I found some code in a page called main.aspx?e=nauqov2blnhlnh45eseztnao that looks also related to the DojoComboBox:
<select name="txtCaseName" id="txtCaseName" class="dropSel"dojotype="ComboBox"
setSelectedValue="dojo.byId('txtCaseName1').value=arguments[0]"
autocomplete="true" dataurl="CaseCode.aspx?match=%{searchString}&lang=e"
mode="remote" maxlistlength="7">
</select>
However, neither of these seem to encode options that I can select. I thought that perhaps the options would be stored in the url labeled 'dataurl' in the piece of HTML above, but when I copy that link into my address bar, it doesn't take me to a valid webpage. From what I've found in the Dojo documentation, it seems like the options should be stored in a separate file/database, but I can't find any suggestions about how to access that file.
Any suggestions? (Thanks!)
you will have to store every options in a list. Then based on some conditions such as name of option, you can choose it and click on it.
all_options = browser.find_elements_by_css_selector("your locator for every options")
for options in all_options:
if "option_name" in options.text.strip():
options.click()

How do i refer to this element with Selenium (python)?

So i'm trying to automate this simple thing, where i want to click on a image.
Here is HTML:
<div class="guild-inner" draggable="false" style="border-radius: 15px;">
<a draggable="false" class="avatar-small" href="/channels/223259649967652887/381675339702599690" style="background-image: url("https://cdn.discordapp.com/icons/223259649967652887/2be8d9ec8759056fabe8caa306c632c8.png");">
</a>
</div>
Here is my code:
def find_tc():
driver.find_element_by_xpath('//a[#href="/channels/223259649967652887/381675339702599690"]').click()
wait(5)
Here is the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[href="/channels/223259649967652887/381675339702599690"]"}
I have to refer this by href because that is the only thing that is unique. All the images are dynamically defined so i also can't refer to the image by on which number it is.Thanks in advance! (new to selenium)
Ok, so the problem is that i was trying to find the element when the page is not completely loaded. So i had to put like one minute sleep time before initiating the search for the element..because the java script is still being loaded in the background. Still is there any was to decide if the page has been completely loaded including java script instead of putting a random sleep time? happy to learn!

Selenium Webdriver python XPath not working

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>What is phishing?</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 via email.</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.
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")
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.
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.
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

For a datepicker, why does one code snippet work, but the other doesn't?

I want to populate two date fields on the same form, using "Today's date".
Both fields are read-only, to force the user to use the datepicker.
I can populate the first date field without problem.
Here is the HTML:
<div class="">
<div id="EffectiveFrom$CalendarBehavior_today" class="ajax__calendar_footer ajax__calendar_today"></div>
</div>
Here is my code:
#browser.div(:id => 'EffectiveFrom$CalendarBehavior_today', :class => 'ajax__calendar_footer ajax__calendar_today').click
Second date field
HTML:
<div class="">
<div id="RegistrationDate$CalendarBehavior_today" class="ajax__calendar_footer ajax__calendar_today">
Today: July 15, 2014
</div>
</div>
Code:
#browser.div(:id => 'RegistrationDate$CalendarBehavior_today', :class => 'ajax__calendar_footer ajax__calendar_today').click
The error is "Element not visible". But I can see exactly the same popup for the second field as I do for the first field.
(Edited) The popup that caused the "Element not visible" error message is fully displayed within the browser boundaries.
It seems to me (a non-techie) that the only difference between the first HTML and the second HTML is that the second HTML has inner text.
The element not visible exception means that the element exists on the page but is not displayed (eg has a style="display:none"). Given that you can see the popup, it is likely a timing issue.
What happens is:
You click something to trigger the popup to display.
Watir tries to click the div element.
The browser finishes updating the popup's style such that it is now visible.
Given that Watir tries to click the div before it gets displayed, you get the element not visible exception.
You can tell Watir to wait for the element to become visible before trying to interact with it:
#browser.div(:id => 'RegistrationDate$CalendarBehavior_today', :class => 'ajax__calendar_footer ajax__calendar_today').when_present.click

Resources