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.
Related
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.
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!
This is the button I'm trying to click:
<button class="stkv-c-button stkv-us-button-color--background
stkv-u-background-color-transition-150
stkv-us-button-color--border stkv-us-button-color
stkv-us-button-color--fill stkv-c-button
stkv-c-button--raised stkv-c-button--big">
<span class="stkv-c-button__label stkv-c-button__label--big">Vote Now</span>
</button>
This is the xpath (pulled from Firefox):
[#id='root']/html/body/div/div/div[3]/div/div/footer/div/button"
I've tried a wide variety of ways to click on the button, all to no avail.
self.driver.find_element_by_xpath("//*[#id='root']/html/body/div/div/div[3]/div/div/footer/div/button").click()
self.driver.findElement(By.id("Vote Now")).click()
self.driver.find_element_by_name("Vote").send_keys(Keys.ENTER)
self.driver.find_element_by_id('stkv-c-button stkv-us-button-color--background stkv-u-background-color-transition-150 stkv-us-button-color--border stkv-us-button-color stkv-us-button-color--fill stkv-c-button stkv-c-button--raised stkv-c-button--big').click()
Any suggestions would be greatly appreciated!
I'm not having any luck with it.
You can find the button by display text on it.
locator = "//button[span[text()='Vote Now']]"
self.driver.find_element_by_xpath(locator).click()
Your button doesn't have an id or name, so the find_element_by_name and find_element_by_id methods will not work. find_element_by_class_name seems like the logical choice, but unfortunately it only expects only 1 class, and since your button has multiple classes, it won't work either. I would try locating the button with its CSS selector:
self.driver.find_element_by_css_selector('button.stkv-c-button.stkv-us-button-color--background.stkv-u-background-color-transition-150.stkv-us-button-color--border.stkv-us-button-color.stkv-us-button-color--fill.stkv-c-button.stkv-c-button--raised.stkv-c-button--big').click()
As long as there isn't another button on the page with the exact same CSS selector, this should give you the correct button. In general, if you want to find an element that has multiple classes, e.g. <button class="A B C">MyButton</button>, then you could do it with:
self.driver.find_element_by_css_selector('button.A.B.C')
Use this XPath : //button[normalize-space()='Vote Now']
As per the HTML you have shared to click() on the button with text as Vote Now you can use either of the following solution:
CSS_SELECTOR:
driver.find_element_by_xpath("span.stkv-c-button__label.stkv-c-button__label--big").click()
XPATH:
driver.find_element_by_css_selector("//span[#class='stkv-c-button__label stkv-c-button__label--big' and contains(.,'Vote Now')]").click()
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
I am working on a selenium script in Python, where I am on this stage trying to locate a submit button.
HTML
<div class="submit-buttons">
<button class="submit" type="submit">Filter</button>
</div>
I've tried and this has not worked.
So I am out of solutions:
browser.find_element_by_partial_link_text('Filter').click()
browser.find_element_by_link_text('Filter').click()
browser.find_element_by_class_name('submit').click()
Try xpath solution:
driver.find_element_by_xpath('//div[#class="submit-buttons"]/button[#class="submit"]')
If it is still not identifying, the element might be inside a frame, and you have to switch to that frame before finding the element.
By link text or by partial link text locators are going to work with links only - a elements. Here you have a button. If you want to use the button text, use the following "by xpath" locator:
//button[. = "Filter"]
This code should work :
driver.find_element_by_xpath('//button[text()='Filter']')
driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();