Python Selenium Editing starred out password field - python

Have a few issues clicking on a password field for my router web interface page. Below are 2 examples.
A: I get no errors, however the password field does not become show the password like when I manually click
B: I get a element not clickable or with a slight change element not interactable
My question is what am I missing here.
Below is the elements from the page
<div class="controls">
<input type="password" id="wrlPwd" name="wrlPwd" class="validatebox input-large" maxlength="63" data-options="{"type":"ssidPwd","args":[8,63]}">
</div>
And below the 2 parts of code
A: PsWrD = browser.find_element_by_class_name('control-label')
B: PsWrD = browser.find_element_by_xpath('//div[#id="id="wrlPwd"" and #class="controls"]') browser.execute_script("arguments[0].click();", PsWrD)
The goal is not to view the password but highlight the text field to change the password. I attempted a few other things such as css and name. I am able to navigate every where else but I am stuck on this little bit.

The issue here was my HTML Knowledge. The issue was my element was in an iframe and so could not find it. To resolve I used:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"membeeLoginIF")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.textboxWaterMark#txtUserName"))).send_keys("Jeff")```
Once I switched to the iframe i found it with out issue

The element you are trying to click is
<input type="password" id="wrlPwd" name="wrlPwd" class="validatebox input-large" ...>
Always start with ID or name, if they are available. This one has both so lets use ID
browser.find_element_by_id("wrlPwd").click()
The locators you are using are not correct.
A: There is no class, control-label, on the INPUT element you posted. The class is class="validatebox input-large" and the classes are validatebox and input-large.
B: Same problem with this one... you are confusing elements or something. The XPath you are using, //div[#id="id="wrlPwd"" and #class="controls"] has a couple issues.
You've mangled the id in there twice, #id="id="wrlPwd"", which should have thrown an invalid XPath error, and the DIV does not have that ID, the INPUT does.
The second part, and #class="controls", is correct for the DIV but you want to click the INPUT.
Additionally, you may want to add a WebDriverWait to wait for the INPUT to be clickable if there are timing issues. See the docs for more info.

Related

Python Selenium - Getting TimeoutException for elements belonging to a particular section of a webpage

I've been trying to click this particular button that belongs to a toggle switch in a website.
<button _ngcontent-plj-c265="" type="button" class="glyphicon glyph-mini ng-star-inserted expand" aria-expanded="true" aria-label="Title Expand or collapse filter card" style="visibility: visible;" pbi-focus-tracker-idx="6"></button>
This exists in the "filter" section of the website and I've adopted several methods and tags to perform this operation, some of which I've put here:
1. sample1 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("xpath", '//*[#id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]'))).click()
2. sample2 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("css selector", 'button.expand'))).click()
3.sample3 = driver.find_element("xpath",'//[#id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]').click()
Method 3 gave me a NoSuchElementException. Hence, I adopted methods 1 and 2 but got a TimeoutException for both. The button has no ID, so I couldn't fetch it that way, too.
But what I noticed was that there is an attribute in the button, aria-expanded="true" which when, I'm assuming, taking a value "false" will help operate the toggle button. But the catch is it can only do that if Selenium identifies the element itself, which brings us back to square one.
I'd highly appreciate any fruitful answer for my predicament. Thanks in advance.
I was able to solve my problem by switching to the <iframe> the element was within using this:
driver.switch_to.frame(0) #switching to the iframe here
tb = driver.find_element("xpath", '//*[#id="exploreFilterContainer"]/div[2]/div/filter[15]/div/div[1]/div[1]/button[1]')
driver.execute_script("arguments[0].click();", tb) #using JS to click the button
and it's advised that after all the operations pertaining to the element is performed that we switch back to normal as:
driver.switch_to.default_content()
Thanks to #Prophet #simpleApp for your efforts in trying to help me out :)

Find xpath with on webpage selenium python

I am having trouble locking down an xpath on a website.
Normally I would just use wait for the path to be clickable and then use it like so:
wait.until(EC.element_to_be_clickable((By.XPATH, ('//input[#value="2~Replace#emailhere.com"]'))))
However the email Replace#emailhere.com changes every time based on what we specify. So I am going to have that in a variable like so: namevar = "Replace#emailhere.com"
But the 2- in front of the email is the row that the email appears on the wesbite. I want to try and find the input button on the website based on the email address alone since the row maybe different.
If I try this, it fails:
###Code above this###
testvar = "*-Replace#emailhere.com"
im_blacklistaddbutton = browser_options.browser.find_element_by_xpath(('//input[#value="'+testvar+'"]'))
im_blacklistaddbutton.send_keys(Keys.SPACE)
I get this failure:
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#value="*-Replace#emailhere.com"]"}
Here is what the element structure is like:
<input type="checkbox" value="2~Replace#emailhere.com" name="uxLvwList_lstChk_0">
Here is a picture of what the webpage and element looks like:
Structure the predicate in your XPath expression to use the contains() function instead of an equality condition:
//input[contains(#value, '-Replace#emailhere.com')]

how to resolve changes in Element's id in html?

After deployement id of elements are changed. So that can not select those element by id in selenium using python.
Suppose I want to find the below element in HTML.
<input class="o_form_input c_field-65 o_form_field o_form_required" id="o_field_input_22" type="text">
I can not use the element's class because there will be elements with the same class.
I would like to find element without using Xpath because if new fields are added in the development side then Xpath will be changed.
In that case you should not be locating your element by id (since it not steady).
For example you can use Xpath (other locators may also work):
login_form = driver.find_element_by_xpath("//form[#id='loginForm']")
or
username = driver.find_element_by_xpath("//form[#id='loginForm']/input[1]")
Now the only thing that you can't copy from this, is the xpath it self. That is specific to your website/html/DOM.
An easy way of getting a correct xpath is by inspecting elements using f12 and then right click on the element, go to copy and select [copy xpath]. You can than paste it into your code.
Let me know it this was helpfull!

'Element Not Visible Exception' when trying to click on a Search option using webdriver selenium in python

I get an 'Element Not Visible Exception' when trying to click on a Search option on a webpage. The element is not hidden, and I have put a time.sleep(10) so the page has enough time to load. Please suggest why I am getting this error and how I can get around it.
I want to click on 'New Search' option in the code.
<a class="newsearch btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBnewsearch"><img src="../../../../resources/images/mt_sprites.gif"
alt="New search" />
</div>
<span>New search</span>
</a>
Please find my code for clicking on it below :
time.sleep(10)
New_Search = browser.find_element_by_css_selector(' #Toolbar > table > tbody > tr > td.TBGroup.TBGroup1 > a.newsearch.btn.btn3d.tbbtn')
action2 = ActionChains(browser)
action2.move_to_element(New_Search).click()
action2.perform()
I've also tried doing a simple find and click on the element but get the same exception at the New_Search.click() step.
time.sleep(15)
New_Search = browser.find_element_by_xpath('//*[#id="Toolbar"]/table/tbody/tr/td[2]/a[1]')
New_Search.click()
I've tried using WebDriverWait as suggested by Debanjan below, but the expected condition isn't satisfied and I get a timeout exception.
time.sleep(15)
WebDriverWait(browser, 15).until(EC.visibility_of_element_located(browser.find_element_by_xpath('//*[#id="Toolbar"]/table/tbody/tr/td[2]/a[1]')));
New_Search.click()
Can you test these locators in the Dev tools and check if they are pointing to the right element on the page? In the console use $x() to test XPaths and $$() to test CSS selectors. If it returns 0, you know there's something wrong with your locator. If it returns 1, you are good to go. If it returns more than 1, you will need to verify that the element you are looking for is the first element returned. If it's not, you will need to craft a new locator. I have a feeling that your current locators is returning 2 nodes, out of which the first one is invisible.
Try to access the target element using that elements ancestors. Keep going one level up till you find a node that is uniquely identifiable and then use relative xpath (//) to traverse down to your element. You can also use ordinals/index to locate the element, but i would not recommend it as they will make your test brittle. If possible update the question with a bigger snippet of the HTML or the applications URL

find submit button in selenium without id

I have a button
<input type="submit" class="button button_main" style="margin-left: 1.5rem;" value="something">
I cannot find it by id or name and need to submit a form.
I tried doing this:
Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised:
element.submit()
http://selenium-python.readthedocs.org/navigating.html
But that cannot find the submit selector either.
ideas?
There are many options here, to name a few:
If class alone is unique, you can use
driver.find_element_by_css_selector(".button_main").click()
If class + value combo is unique, you can use:
driver.find_element_by_css_selector(".button_main[value='something']").click()
You can also use xpath:
driver.find_element_by_xpath("//input[#type='submit' and #value='something']").click()
If none of those work (i.e. they are not identifying button uniquely), look at the elements above the button (for example <form) and provide the xpath in format:
driver.find_element_by_xpath("//unique_parent//input[#type="submit" and #value='something']").click()
i recommend xpath chrome extension, with it you will be able to get the path by running the extension and shift-clicking on the element you want this chrome extension https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl
You could try to find the element with an XPath expression or a CSS selector like input[type="button"], and then just click the element.

Categories

Resources