unable to locate element : selenium in python - 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")

Related

Finding a submit button with Selenium in Python3

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

Unable to select Angular dropdown menu using selenium in python

A selenium newbie here. I'm trying to use Selenium to automate download of CSV file from calltools and upload it to salesforce. Here is the UI for the dropdown menu for selecting the options before exporting the CSV.
The HTML for the same is:
<div _ngcontent-rnw-c215="" style="">
<label _ngcontent-rnw-c215="">Templates</label>
<br _ngcontent-rnw-c215="">
<select _ngcontent-rnw-c215="" class="ng-pristine ng-valid ng-touched">
<option _ngcontent-rnw-c215="" value="0: null"></option>
<option _ngcontent-rnw-c215="" value="1: Agent KPI" class="ng-star-inserted"
style="" xpath="1">Agent KPI</option>
<option _ngcontent-rnw-c215="" value="2: Payroll" class="ng-star-
inserted">Payroll</option>
<option _ngcontent-rnw-c215="" value="3: Agent Status" class="ng-star-
inserted">Agent Status</option>
<!---->
</select>
I have tried multiple ways to access the appropriate option (1. Agent KPI). My code so far is
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//select[#class='ng-valid ng-touched ng-dirty']//option[#value='1: Agent KPI']"))
).click()
The other approach tried is:
driver.find_element(By.XPATH, "//select[#class='ng-valid ng-touched ng-dirty']//option[#value='1: Agent KPI']")
All these options are resulting in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[#class='ng-valid ng-touched ng-dirty']"}
But this didn't work either. I used implicit wait as well but I am getting the same error. I checked the HTML and there's no iframe within the same. I have been using selectorhub to retrieve XPATHs of elements so pretty sure they're correct. I also used CSS selectors and tag names but still stuck on the same.
I looked everywhere but despite the various threads and answers, I'm still stuck on this issue. Could anyone advise me on what's going wrong here?
Thanks!
It seems you are nearly there. If I take the reference of your page element I can see the class name you have provided seems wrong.
Try this below xpath.
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//select[#class='ng-pristine ng-valid ng-touched']//option[#value='1: Agent KPI']"))
).click()

Unable to locate an element in a span class using Python Selenium

I have been struggling to locate an element in a span class. The element is a radio-checkmark button. Here is the html:
<span class="radio-container" for="searchType_2">
<input class="form-check-input" type="radio" name="searchType" id="searchType_2" value="cidade">
<span class="radio-checkmark">
::after
As the classes above are not unique, I tried the following:
dropdown_menu = self.driver.find_element_by_css_selector('[for="searchType_2"] .radio-checkmark')
When I do the inspection and search using the CSS selector above it works. It shows me as 1 of 1. But when I run the code, I get the following exception:
no such element: Unable to locate element: {"method":"css selector","selector":"[for="searchType_2"] .radio-checkmark"}
(Session info: chrome=92.0.4515.107
Thanks
The element you are trying to access is inside an iframe. You need to switch to the iframe before accessing any element inside it
driver.switch_to_frame(driver.find_element_by_xpath("//iframe[#class='cz-map-frame']"))
driver.find_element_by_xpath("//input[#id='searchType_2']//following::span[#class='radio-checkmark'][1]").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')

Unable to locate element (text-field) in Angular JS using selenium in Python

I'm having a problem sending keys to a text field on an webpage written in angular JS.
Here is the WebElement in question:
<input name="CreateUserForename" id="Textc3829619-ad42-4df7-bbe3-5bdbe9fb9bce"
ng-class="{'ng-pristine': controller.$pristine, 'ng-invalid': controller.$invalid,
'ng-invalid-required': controller.$error.required, 'ng-valid': controller.$valid,
'ng-dirty': controller.$dirty}" class="form-input ng-scope ng-valid ng-dirty" type="text"
ng-if="!multiline" ng-hide="disabled" maxlength="">
Just using driver.find_element_by_xpath({xpath}).send_keys({keys}) gives this error:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="Text9c04f240-66a6-478b-92c2-13bb39379b8e"]"}
Same when using the css_selector.
One workaround I've found is using ActionChains and move_to_element_with_offset, but that is not ideal.
Any ideas?
(please don't suggest the Protractor)
OK, it seems that driver.find_element_by_name("CreateUserForename") does the trick.

Categories

Resources