Finding a submit button with Selenium in Python3 - python

I´m working with a HTML code like this:
<p class="inhaltstext_ueberschrift">Some Text</p><br><p class="inhaltstext">some
Text<span class="inhaltstext">
<b><LI>Some headline</LI></b>
<b><LI>Headline of searched button</LI></b>
<form action="qr_info.php" name="1312" method="post">
<INPUT TYPE="submit" NAME="1314" VALUE="Create Ticket">
My code for finding the Button is:
button = driver.find_element(By.XPATH, '//input[#name="1314"]')
or
button = driver.find_element(By.NAME, '1314')
but this doesn´t work. This is the error Code:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"meth oXPathpath","selector":"//input[#name="1314"]"}
Can someone tell me what I´m getting wrong?

The HTML has an input with the attribute of NAME, but your selector is looking for the attribute of name. Making the case the same in your selector may solve your problem.

The name which was shown in the Source Code of the HTML file was not the real name.
I listed up all Names by:
elements = driver.find_elements(By.XPATH, '//*')
for element in elements:
print(element.get_attribute('name'))
With that, one can easily get the correct names

Related

unable to locate element : selenium in python

I'm a newbie in coding with python and selenium. I try to find an element in a web-page by its class name.
<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf"
autocomplete="username" spellcheck="false" tabindex="0"
aria-label="Adresse e-mail ou numéro de téléphone" name="identifier"
value="" autocapitalize="none" id="identifierId" dir="ltr"
data-initial-dir="ltr" data-initial-value="">
But each time I've tried I have this error.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".whsOnd zHQkBf"}
I don't understand why because I verified and I think that it's not in an iframe so I don't understand why do I have this message and I don't know how to manage the error.
Here are the pictures of my code and the html :
My code
The HTML of the page
Hard to tell without seeing your code and the html. Are you sure it is not in a iframe?
If not you can perhaps try to use xpath to find the element.
When inspecting the webpage in chrome you can right click the element and say copy -> copy xpath. I do recommend looking into xpath, so you know what you are searching for.
Edit:
Try using only one class name, since find_element_by_class_name() only accepts one.
This:
driver.find_element_by_class_name("zHQkBf")
instead of this:
driver.find_element_by_class_name("whsOnd zHQkBf")

Why does this give me an error saying NoSuchElementException when the link text is present in the html?

I'm using selenium to log in into my instagram account but when I try to click the login button I get the error
NoSuchElementException: Message: no such element: Unable to locate element:
I'm trying to find this element
<div class="Igw0E IwRSH eGOV_ _4EzTm">Log In</div>
Shouldn't I be able to use
login = driver.find_element_by_link_text('Log In')
or
login = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.LINK_TEXT, 'Log In')))
to access that element?
Why don't either of them work?
When I saw, the class attribute had the value Igw0E IwRSH eGOV_ _4EzTm
So the line of code in your program won't work because the attribute value is wrong and it changes frequently. I would avoid these alphanumeric values as a reference in any website as majority of them are dynamically generated.
I suggest you to use xpath to find these buttons mostly or id if you could find any. Most of them are not dynamic and also lucky for us they are unique too. This one will work:
driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]/button/div').click()

Selenium driven browser shows different HTML code

I am not a very experienced coder so apologies if I say smth stupid.
I am using Python (in Spyder) to get Selenium to fill in a website form containing username and password. Here's the target - link.
When I lookup the "username" element by pressing F12 in a regular browser I get the following:
<input class="slds-input input" type="text" aria-describedby="" placeholder="Username" id="172:0" data-aura-rendered-by="176:0" data-interactive-lib-uid="2">
So I attempt to locate the element using the ID. However when I run the script, I get the following error in Chrome:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[id="172:0"]"}
Same when I run it in Firefox instead:
NoSuchElementException: Unable to locate element: [id="172:0"]
When I check HTML in the Selenium driven browser, I can see that the page code is (ie element ID) different, as below
<input class="slds-input input" type="text" aria-describedby="" placeholder="Username" id="78:2;a" data-aura-rendered-by="82:2;a" data-interactive-lib-uid="2">
My best guess is that the difference in HTML code is the reason for error. I found people posting similar issues but those were slightly different and I was not able to solve my issue using the solutions proposed there. I would appreciate is someone could help with my case.
Use xpath instead of id since it changes dynamically
Xpath for UserName: //label/following-sibling::input
Xpath for Password: //lightning-input//div//input
Sample working code which works in java convert with using above xpath in python and also add implicitlyWait and pageLoadTimeout befor launching the website
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://lta-tennis.force.com/"); // WebElement
driver.findElement(By.xpath("//label/following-sibling::input")).sendKeys("dummy");
driver.findElement(By.xpath("//lightning-input//div//input")).sendKeys("dummy");
System.out.println(driver.getTitle());
Edit 1: Based on OP comment
This is working xpath
try these xpaths
//input[#placeholder="Username"]
//input[#placeholder="Password"]
here is the full code
from selenium import webdriver
import time
browser = webdriver.Chrome('C:\\driverpath\\chromedriver.exe')
url = 'https://lta-tennis.force.com/s/login/'
get = browser.get(url)
time.sleep(5)
browser.find_element_by_xpath('//input[#placeholder="Username"]').send_keys('hello')
browser.find_element_by_xpath('//input[#placeholder="Password"]').send_keys('pass')

I m not able to click on a button in a web page using selenium in python

What is wrong in the below code
import os
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://x.x.x.x/html/load.jsp")
elm1 = driver.find_element_by_link_text("load")
time.sleep(10)
elm1.click()
time.sleep(30)
driver.close()
The page source is
<body>
<div class="formcenterdiv">
<form class="form" action="../load" method="post">
<header class="formheader">Loader</header>
<div align="center"><button class="formbutton">load</button></div>
</form>
</div>
</body></html>
I want to click on button load. when I ran the above code getting this error
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: load
As the documentation says, find_elements_by_link_text only works on a tags:
Use this when you know link text used within an anchor tag. With this
strategy, the first element with the link text value matching the
location will be returned. If no element has a matching link text
attribute, a NoSuchElementException will be raised.
The solution is to use a different selector like find_element_by_class_name:
elm1 = driver.find_element_by_class_name('formbutton')
Did you try using Xpath?
As the OP said, find_elements_by_link_text works on a tags only:
Below code might help you out
driver.get_element_by_xpath("/html/body/div/form/div/button")

Python3 (Selenium) can't locate password box

my current problem is that Python can't locate my password box.
<input class="stylepwd" name="pws" size="12" maxlength="12" onkeypress="return stEnter(event,this);" autocomplete="off" type="password">
Thats all i tried to do, but it always say it cant locate that box
element = driver.find_element_by_name('pws')
element = driver.find_element_by_class_tag('stylepwd')
element = driver.find_element_by_id('') #Yea, thats obviously not working ^^'
element.send_keys('mypassword')
Maybe there's another way I can type in that password. As soon as the site is loading, the cursor is in the box. Can I do anything with this? (Thats the site: http://imgur.com/a/6OIgm)
Python code: https://pastebin.com/3ydHwDkH
You can do something like this :
elem = driver.find_element_by_xpath('//input[#class="stylepwd"]')
This will give you the element input with class="stylepwd"
If it is in iframe, try
iframe = driver.find_elements_by_tag_name("iframe")[0]
driver.switch_to_frame(iframe)
then
element = driver.find_element_by_name('pws')

Categories

Resources