Python + Selenium: Unable to locate element - python

I'm trying to locate a tab and click it on my webpage. However, my code does not work:
ActionChains(driver).move_to_element(driver.find_element_by_xpath("//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div")).perform()
additionaldata_ele= driver.find_element_by_xpath("//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div").click()
The HTML body is as follows:
<li class="WJX1 WLV1" id="tab_015ba30c-af6c-4c9a-ac34-f77ee00805b6" role="tab" aria-controls="tabPanel_16845ddd-961b-4581-89da-a6a4e6080930" data-automation-id="tab" aria-selected="false"><div class="WGX1"></div><div class="WEX1"><div class="gwt-Label WLX1" data-automation-id="tabLabel">Additional Data</div></div></li>
I get the error -
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div"}
I guess the reason is that when I try to find the element, it doesn't appear on the DOM, so I should implement WebDriverWait until the element is visible. So I tried WebDriverWait, but it didn't work either.
Many thanks for all answers and replies!
second edition:
Here is the webpage, sorry I cannot share the link, it is an internal webpage and PSW is required:
This is the screenshot of the page

That id looks dynamic - it will probably change frequently and result in an unstable script.
Additionally, you will want a wait to ensure your web page is ready before you continue.
try something like this:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Additional Data"]'))).click()

Related

Unable to login site Selenium

I'm trying to do log in into my university site but i receive this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div[2]/div/div/div/div[2]/form/table/tbody/tr[3]/td[2]/input"}
this is the code part:
textboxes3 = browser.find_element_by_xpath("/html/body/div/div/div[2]/div/div/div/div[2]/form/table/tbody/tr[3]/td[2]/input")
textboxes3.click()
textboxes3.send_keys(matricola)
and this is the site inspection:1
Try targeting the element by id:
textboxes3 = browser.find_element_by_id("username")
textboxes3.click()
textboxes3.send_keys(matricola)
Using browser.find_element_by_id("username") should work, but since it's still not finding the element then I suspect one of two issues might be occurring:
The element hasn't loaded yet and selenium needs to wait longer. I recommend using the WebDriverWait class to wait for the element to load.
Websites are divided into iframes, Selenium may be searching for the element in the wrong iframe. If this is the case you will need to use the driver.switch_to.frame function.
I recommend trying this code out first:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
w = WebDriverWait(self.driver, 10)
textboxes3 = w.until(ec.presence_of_element_located((By.ID, 'username')))
textboxes3.click()
textboxes3.send_keys('matricola')

Selenium no such element: Unable to locate element (trying to login)

I am trying to use Selenium to download a file from https://id.opswat.com, in order to access it I need to login, but I can't, because when I try to retrieve the email input, it gives me an error ... This is my code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://id.opswat.com/login')
driver.find_element_by_name('email').send_keys('email#email.es')
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
driver.find_element_by_name('password').send_keys('pass')
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
And this is the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
The login on that page is a bit special, because first you have to enter the email, check that it is well formed, click to continue, and you will see the password entry and the button to log in ...
Maybe something very basic is missing me, but I can't find it ... (I've tried waits, but neither ...)
First, your line
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
will always fail because class_name function does not handle spaces in the class. I'd suggest changing that to
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
Next, You need to wait for the page to load before you try to interact with each field. This works for me:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://id.opswat.com/login')
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.NAME, 'email'))).send_keys('email#email.es')
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.NAME, 'password'))).send_keys('pass')
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
I can't see anything obviously wrong with it, have you checked if you can get the driver.find_element_by_class_name('emailInputBox')? Or any other element in the page?
Does it work when getting by XPath?
//*[#id="root"]/div/section/div/div/div[2]/form/div[2]/div/div/div/input
You could also try waiting for the element to be present like in this answer:https://stackoverflow.com/a/50324722/10832847

Selium Unable to Locate Class Element

Dear Stackoverflowers,
I'm trying to automate a CC payment process but Selenium is having a hard time identifying a specific element I want to click on. I'm trying to click on 'REI Card - 6137' so that I can continue to the payment page. Using the inspect tool it shows the class as, "soloLink accountNamesize". Unfortunately, there's not an ID I can go after. When I try to search by class name I get this error in the console:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: .soloLink accountNamesize
Below is a picture of the site and the inspector pane with the thing I'm trying to click on highlighted in blue. Since its my credit card and I'm already logged it a link to the page wouldn't really help you guys.
The script gets hung up on "driver.find_element_by_class_name('soloLink accountNamesize').click()"
My code is below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import yaml
import time
conf = yaml.load(open(r'D:\Users\Matt\Documents\GitHub\YML_Files\REI_Login_Credentials.yml'))
myREIUsername = conf['REILogin']['username']
myREIPassword = conf['REILogin']['password']
driver = webdriver.Firefox(
executable_path=
r'D:\Users\Matt\Documents\GitHub\Executable_Files\geckodriver.exe'
)
def login():
driver.get('https://onlinebanking.usbank.com/Auth/Login?usertype=REIMC&redirect=login&lang=en&exp=')
time.sleep(4)
driver.find_element_by_id('aw-personal-id').send_keys(myREIUsername)
driver.find_element_by_id('aw-password').send_keys(myREIPassword)
time.sleep(2)
driver.find_element_by_id('aw-log-in').click()
time.sleep(15)
make_payment()
def make_payment():
if (driver.find_element_by_class_name("accountRowLast").text) != "0.00":
driver.find_element_by_class_name('soloLink accountNamesize').click()
else:
driver.quit()
I've tried searching by Xpath and Xpath + Class with no luck. I also tried searching for this issue but its a fairly unique class so I didn't have much luck. Have any other ideas I could try?
soloLink accountNamesize is multiple class names use the following css selector instead to click on that element.
driver.find_element_by_css_selector('a.soloLink.accountNamesize').click()
To induce waits we do
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.soloLink.accountNamesize"))).click()
Based on the photo, I think that this is the xpath that you might want
//div[#id='MyAccountsDiv']//div[#id='CreditsTableDiv']//tbody//tr[#class='accountRowFirst']//a[contains(#onclick, 'OpenAccountDashboard')]
As you can see, this xpath starts off with the top-most div that might be unique ( MyAccountsDiv ) and continues to dive into the HTML code.
Based off of this, you could click on the link with the following code
xpath = "//div[#id='MyAccountsDiv']//div[#id='CreditsTableDiv']//tbody//tr[#class='accountRowFirst']//a[contains(#onclick, 'OpenAccountDashboard')]"
driver.find_element(By.XPATH, xpath).click()
NOTE
Your error says
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="aw-personal-id"]
Maybe you can use the above technique and see if you can isolate the xpath for the web element instead.

Send keys to HTML element using Selenium

I am writing an automation script for sports betting in Python using Selenium. I am stuck at a point where Selenium is unable to click or send keys to the specific HTML element highlighted in the following screenshot (https://i.stack.imgur.com/NbljY.png).
Here is what I have tried:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.bet365.com")
### Some code here to navigate to a particular match
driver.switch_to.frame(bet_driver.find_element_by_tag_name("iframe"))
elem = driver.find_element_by_class_name("bs-Stake")
elem.click()
elem.send_keys("100")
This returns the following error:
ElementNotInteractableException: Element <div class="bs-Stake"> is not reachable by keyboard
If I try instead
elem = driver.find_element_by_class_name("stk bs-Stake_TextBox")
I get the error:
NoSuchElementException: Unable to locate element: .stk bs-Stake_TextBox
I would appreciate help navigating to the HTML element, clicking and sending keys to it, using any method available in Selenium.
Try to sendkeys with webdriver wait so element would be able to interact.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 50).until(EC.visibility_of_element_located((CSS_SELECTOR, ".stk.bs-Stake_TextBox")))
element.send_keys("100")
Although CSS styles are inherited... the class designations are not. Your css selector has three separate classes in it, but your element sample only has one.
Selenium is designed to mimic the same actions that a person can perform... the error suggests that the field is not available for interaction.... find out why.

Unable to locate element within an iframe through Selenium

I'm new to Selenium. I'm trying to write a Python script that will log in to a particular form. The form is located at http://www.tvta.ca/securedContent
The code I'm running is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://tvta.ca/securedContent/")
elem = driver.find_element_by_name("txtUserName")
elem.clear()
elem.send_keys("<<my email>>")
I get an error that says:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="txtUserName"]
I'm not sure what I'm missing here? I've checked the source of the webpage the login field is definitely named txtUserName.
You need to switch to the frame to write text in the textbox, try to check syntax once as I have less in good in Python
framLogin= driver.find_element_by_id("membeeLoginIF")
driver.switch_to.frame(framLogin)
EmailTxt = driver.find_element_by_name("txtUserName");
EmailTxt.send_Keys("Test#gmail.com");
Same in Java
WebElement framLogin= driver.findElement(By.id("membeeLoginIF"));
driver.switchTo().frame(framLogin);
WebElement EmailTxt = driver.findElement(By.name("txtUserName"));
EmailTxt.sendKeys("Test#gmail.com");
That site requires third-party cookies to be enabled; without them, the login form does not load. It's likely you have your browser configured that way but the defaults for webdriver.Firefox do not.
To see what Selenium is actually seeing, dump driver.page_source and/or take a screenshot with driver.save_screenshot(...).
The desired element is within an <iframe>. So as per best practices you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable and you can use the following Locator Strategies:
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")
You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories