NoSuchElementException error when trying to find by element by ID - python

I am very new to Selenium so any help would be appreciated! I am trying to click the button "continue" in this page and it kept showing me:
"NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[id="bktContinue"]"}".
How do I get it to click "continue"?
Code trials:
url="https://www.exteriores.gob.es/Consulados/toronto/en/ServiciosConsulares/Paginas/Consular/Visados-Schengen.aspx"
driver.get(url)
continue_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'HERE')
continue_link.click()
driver.implicitly_wait(15)
l=driver.find_element(By.ID,'bktContinue')
Snapshot of the HTML:
Here's the HTML I want it to click on:
<div id="dialog-confirm" title="Important Notice / Important Notice" style="font-size: 15px; background-color:#F3F5F7; height: 100%; width: 94%; box-sizing: border-box; color:black !important;">
<div style="text-align: center;">
<span style="color: #ff0000; font-weight: bold;"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">ATTENTION READ THIS MESSAGE CAREFULLY</font></font></span>
</div>
<br>
<div style="text-align: justify;">
<div style="text-align: justify;"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
• Enter the data requested, correct and complete. </font></font><br>
<br><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
• Do not use initials for the identifying fields or we will reject your request. </font></font><br>
<br><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
• </font></font><span style="text-decoration: underline"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">The request for a minor's passport renewal must be made with the minor's passport data. </font></font></span><br>
<br><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
• The information you provide is the one that will appear in the passport. </font><font style="vertical-align: inherit;">Pay attention. </font></font><br>
<br><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
Those appointments with incomplete or incorrect data will be canceled by the system. </font></font><br><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
A person whose identity cannot be verified will not be able to access the facilities, having to request a new appointment with correct identification data.</font></font><br>
<br>
</div>
<br>
<div style="text-align: justify; font-weight: bold;"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
CONTINUING CONSTITUTES ACCEPTANCE OF THE ABOVE INDICATED RULES.
</font></font></div>
</div>
<br>
<div id="bktContinue" style="text-align: center;">
<div style="color: #ffffff; padding: 5px 10px; background-color: #99cc00; display: inline-block;"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
CONTINUE
</font></font></div>
</div>
Snapshot of the element:
This is what I currently have. To get to the website I am having trouble with, you have to go in this website first:
https://www.exteriores.gob.es/Consulados/toronto/en/ServiciosConsulares/Paginas/Consular/Visados-Schengen.aspx
Then click "book your appointment here"
How do I get it to click "continue" after this?

Try using this CSS selector instead:
continue_div = driver.find_element(By.CSS_SELECTOR,'#bktContinue > div')
My assumption is that the reason the continue button is not being pressed, is because the real element that should be clicked is the div inside of the element with the ID bktContinue.
Also another tip: It's possible that you're not clicking the correct link at this line:
continue_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'HERE')
continue_link.click()
Because you're looking for a link element that contains the text 'HERE', there are 4 elements that match this criteria, so your scriptelement might be clicking another link where the #bktContinue is not on the next page and this might also be leading to a NoSuchElementException.

To click on the element with text as CONTINUE you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#bktContinue font"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='bktContinue']//font[contains(., 'CONTINUE')]"))).click()
Note: 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

Related

Selenium webdriver can't find visible element

I have a problem with finding element on a webpage. It's a popup window with a confirmation [NO]/[YES]. Selenium can find a parent of a buttons but not a button itself.
Popup window view
HTML block of code:
<div tabindex="-1" role="alertdialog" style="position: fixed; height: auto; width: 300px; top: 188.5px; left: 800px; max-width: 100%;"
class="ui-dialog ui-dialog--notification ui-dialog--modern ui-widget ui-widget-content ui-front ui-dialog-buttons ui-draggable"
aria-describedby="ui-id-3" aria-labelledby="ui-id-4">
<div class="ui-dialog-titlebar ui-corner-all ui-widget-header ui-helper-clearfix ui-draggable-handle"></div>
<div class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: 32px;">
<div class="a-AlertMessage">
<div class="a-AlertMessage-body">
<div class="a-AlertMessage-details" id="ui-id-3">Czy chcesz wygenerować pozycje dla całej dostawy?</div>
</div>
</div>
</div>
<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix"><div
class="ui-dialog-buttonset">
<button type="button" class="ui-button ui-corner-all ui-widget">Nie</button>
<button type="button" class="js-confirmBtn ui-button ui-corner-all ui-widget ui-button--hot">Tak</button>
</div>
</div>
</div>
Here is a code snippet that selenium can't find and gives an error 'Unable to locate element':
pop_up_yes_button = driver.find_element(By.XPATH, '//*[#id="t_PageBody"]/div[15]/div[3]/div/button[2]').click()
Parent is avaible for selenium without a problem:
driver.find_element(By.XPATH, '//*[#id="t_PageBody"]/div[15]')
I also tried WebDriverWait and ExpectedConditions but it gives TimeoutException:
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, '//*[#id="t_PageBody"]/div[15]/div[3]/div/button[2]'))).click()
As you said that it is a popup window, it is very likely that the button is in an iFrame. You have to wait until the iFrame loads and then search the element again.
Luckily, selenium has a built in function for dealing with iFrames:
your_button = WebDriverWait(driver,
20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//*[#id="t_PageBody"]/div[15]/div[3]/div/button[2]')))
your_button.click()
Don't just copy and paste this code, understand this thing and implement it.

Selenium: Can't open link to download a file (Python)

I'm trying to automate downloading a report, but my script is refusing to find the element to click on...
def wait_click(browser, by_what, where):
wait = WebDriverWait(browser, 20)
wait.until(EC.element_to_be_clickable((by_what, where))).click()
tillf_export = '//*[#id="panel__6940363340341543984_linkb_6940363340341543984"]'
# inside class
def download_tillf(self):
self.browser.get(system_export)
wait_click(self.browser, By.XPATH, tillf_export)
I've tried every possible way, switching away from XPATH, using parent elements and even execute_script with the js.
This is the div:
<table class="dxrpControl" cellspacing="0" cellpadding="0" id="panel__6940363340341543984" border="0" style="border-collapse:collapse;border-collapse:separate;">
<tbody><tr>
<td id="panel__6940363340341543984_HC" class="dxrpHeader dxrp-headerClickable dx-borderBox" style="text-align: center; border-bottom: 1.33333px solid rgb(198, 198, 198); border-bottom-left-radius: 0px; border-bottom-right-radius: 0px;"><div id="panel__6940363340341543984_CB" class="dxrpCollapseButton" style="margin-top: 1px;">
<img id="panel__6940363340341543984_CBImg" class="dxWeb_rpCollapseButton" src="/test/jlltp02/sjalvservicerdr/DXR.axd?r=1_89-UNelo" alt="Collapse/Expand">
</div><div class="dxrpHCW" style="padding-right: 19px;">
** <span id="panel__6940363340341543984_RPHT" class="dxrpHT dx-vam">Tillfällig export</span>
</div></td>
</tr><tr class="dxrpCR">
<td id="panel__6940363340341543984_RPC" class="dxrp dxrpcontent dx-borderBox"><div class="dxrpAW" style="">
<div id="panel__6940363340341543984_CRC" class="dx-borderBox dxrpCW">
<table border="0">
<tbody><tr>
<td><span class="UTDATANODTITLEDESC">Tillfällig export kan användas för att snabbt ta ned svaret på en fråga genom att bara ändra den i "Automatiska frågor". Detta för att slippa gå in i Citrix och spara ned filen på M:.</span></td>
</tr><tr>
<td valign="top"><a id="panel__6940363340341543984_linkb_6940363340341543984" class="UTDATANODLINK" href="javascript:outputClick('6940363340341543984');">>> Tillfällig export</a></td>
</tr>
</tbody></table>
</div>
</div></td>
</tr>
</tbody></table>
Can anyone see what I'm doing wrong? Would be forever grateful!
The id attribute of the desired element looks dynamic and the element itself is possibly a dynamic element so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using PARTIAL_LINK_TEXT:
Tillfällig export
Using CSS_SELECTOR:
a.UTDATANODLINK[id*='linkb']
Using XPATH:
//a[#class='UTDATANODLINK' and contains(., 'Tillfällig export')]

Selenium + Python: Print the text attribute of an element

I would like to navigate through a website, find an element and print it.
Python version: 3.10; Selenium Webdriver: Firefox; IDE: PyCharm 2021.3.2 (CE);
OS: Fedora 35 VM
I am able to navigate to the appropriate page where the text is generated in a drop down menu.
When I locate the element by CSS Selector and attempt to print it, the output does print the text "None".
I would like it to print the Plan Name which in this case is "Dual Complete Plan 1".
The element is not always present so I also need to catch any exceptions.
The relevant HTML code of the element I am trying to print:
<span class="OSFillParent" data-expression="" style="font-size: 12px; margin-top: 5px;">Dual Complete Plan 1</span>
More of the HTML code of the element I am trying to print (element I am trying to capture is below the fourth div):
<td data-header="Plan Name">
<div id="b8-b40-l1_0-132_0-$b2" class="OSBlockWidget" data-block="Content.AccordionItem">
<div id="b8-b40-l1_0-132_0-b2-SectionItem" class="section-expandable open is--open small-accordion" data-container="" data-expanded="true" aria-expanded="true" aria-disabled="false" role="tab">
<div id="b8-b40-l1_0-132_0-b2-TitleWrapper" class="section-expandable-title" data-container="" style="cursor: pointer;" role+"button" aria-hidden="false" aria-expanmded="true" tabindex="0" aria-controls="b8-b40-l1_0-132_0-b2-Content" EVENT FLEX
<div id="b8-b40-l1_0-132_0-b2-Title" class="dividers full-width">
<span class="OSFillParent" data-expression="" style="font-size: 12px; margin-top: 5px;">Dual Complete Plan 1</span>
</div>
<div class="section-expandable-icon" data-container="" aria-hidden="true"
::after
</div>
</div>
<div id="b8-b40-l1_0-132_0-b2-ContentWrapper" class="section-expandable-content no-padding is--expanded" data-container="" tabindex="0" aria-hidden="false" aria-labelledby="b8-b40-l1_0-132_0-b2-TitleWrapper">
<div id="b8-b40-l1_0-132_0-b2-Content" role="tabpanel">
<a data-link="" href="https://www.communityplan.com" target="_blank" title="Click for more information"> EVENT
<span class="OSFillParent" data-expression="" style="font-size: 12px;">www.CommunityPlan.com</span>
</a>
<span class="OSFillParent" data-expression="" style="font-size: 12px:">Phone Number: 8005224700</span>
</div>
</div>
</div>
</div>
</td>
My relevant Selenium code:
# Find the Plan Name & if present set it to the variable "Advantage"
try:
Advantage = (WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#b8-b40-l1_0-132_0-b2-Title > span:nth-child(1)"))).get_attribute("value"))
except:
pass
print('\033[91;46m', Advantage, '\033[0m')
I expect the output to be "Dual Complete Plan 1", which is what I see on the screen and in the HTML. Instead I get the following:
None
Apparently the "Advantage" variable is being set to "None".
Why?
I can see the text "Dual Complete Plan 1" that I want to print in the HTML code above.
What am I doing wrong?
I feel like I need a primer on "get attribute"?
To get the text Dual Complete Plan 1 you need to use
element.text
or
element.get_attribute("innerHTML")
or
element.get_attribute("textContent")
Instead of presence_of_element_located() use visibility_of_element_located()
and following css selector to identify
div[id*='Title'] > span.OSFillParent
Or
div.dividers.full-width > span.OSFillParent
Code:
try:
Advantage = WebDriverWait(driver, 5).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "div[id*='Title'] > span.OSFillParent"))).text
except:
pass
print(Advantage )

Why cant I find the element of credit card number using selenium?

I am trying to make an automated checkout system but I can't seem to send keys in fields of this payment box. Even when I try implicit wait and wait for it to load in it still doesn't work.
checkout = driver.find_element_by_id("number")
checkout.send_keys("9848432")
<input required="" autocomplete="cc-number" id="number" name="number" type="tel" aria-describedby="error-for-number tooltip-for-number" data-current-field="number" placeholder="Card number" style="font-family: Roboto, sans-serif; padding: 0.94em 0.8em; transition: padding 0.2s ease-out 0s;">
Here's a link!
Here is the URL you need to fill in the info until you get to the payment page, which is where I am having the issues
It might be in a nested iframe. A similar checkout due to not proper reproducible code.
<iframe src="https://fs.global-e.com/Checkout/v2/598d87e9-cd85-4f61-84df-81e345d62f05?gaSesID=526107767.603750114.583&gaMerchantClientInfo=undefined#undefined&chkcuid=b51f4449-13e2-4883-92ba-2641bb06ec51&isNECAllowed=true&vph=663&ift=87" class="Intrnl_CO_Container" id="Intrnl_CO_Container" name="Intrnl_CO_Container" allowtransparency="true" width="100%" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" height="1000px" style="height: 3101px;">
<iframe id="secureWindow" class="clear-fix clearfix" allowtransparency="true" style="width: 100%; min-height: 160px; height: 165px;" src="https://securev2.global-e.com/payments/CreditCardForm/598d87e9-cd85-4f61-84df-81e345d62f05/11">
<input autocomplete="off" class="form-control input-validation-error" data-type="unknown" data-type-id="1" data-val="true" data-val-luhn="Card number not valid" data-val-luhn-allowempty="False" data-val-luhn-allowspaces="False" data-val-required="Card number" id="cardNum" name="PaymentData.cardNum" pattern="[0-9]{13,16}" placeholder="Card number" type="tel" value="">
</iframe>
</iframe>
Which was solved with:
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'Intrnl_CO_Container')))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'secureWindow')))
checkout = wait.until(EC.presence_of_element_located((By.ID, "cardNum")))
checkout.send_keys("9848432")
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

seleniumpython dropdown list div

i whould like to select the element "aout" for example on the folloing dropdown list with python selenium
<div class="questionDropdownContainer inlineBlock isActive" id="dob2_m">
<span class="questionDropdownPlaceholder" data-val="3" id="dateMonth" style="color: rgb(0, 0, 0);">mars</span>
<span class="iconDropdown inlineBlock valignMiddle"></span>
<div class="questionDropdownOptions ps-container ps-active-x ps-active-y isActiveSurveyDropDown" style="display: block; overflow: hidden;">
<span class="option-1-month" data-id="1">janvier</span>
<span class="option-1-month" data-id="2">février</span>
<span class="option-1-month selected" data-id="3">mars</span>
<span class="option-1-month" data-id="4">avril</span>
<span class="option-1-month" data-id="5">mai</span>
<span class="option-1-month" data-id="6">juin</span>
<span class="option-1-month" data-id="7">juillet</span>
<span class="option-1-month" data-id="8">août</span>
<span class="option-1-month" data-id="9">septembre</span>
<span class="option-1-month" data-id="10">octobre</span>
<span class="option-1-month" data-id="11">novembre</span>
<span class="option-1-month" data-id="12">décembre</span>
<div class="ps-scrollbar-x-rail" style="width: 120px; left: 0px; bottom: 3px;"><div class="ps-scrollbar-x" style="left: 0px; width: 110px;"></div></div><div class="ps-scrollbar-y-rail" style="top: 0px; height: 234px; right: 3px;"><div class="ps-scrollbar-y" style="top: 0px; height: 116px;"></div></div></div>
<input type="hidden" name="date_m" value="4">
</div>
When I try this code…
driver.find_element_by_id("dob2_m").click()
driver.find_element_by_id("dob2_m").find_element_by_xpath('//*[#data-id="8"]').click()
I get this error…
error script picture
How to do it ?
Help me please
Thank you
The error snippet clearly says the error occurs when webdriver tries to interact with element with class="option-1-day". But this element is not present in the code you have shared. So i assume, you are facing issue while trying to select date from dropdown.
1.Before clicking dropdown element, try to add wait statement
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))
2. It will occur when the webelement you trying to click, located at end of dropdown. So it can only clickable once scrolling it down.
Actions action = new Actions(driver);
action.moveToElement(element);
Use select class for handling dropdown.
s1= Select(driver.find_element_by_id('id_of_your_element'))//in this case it would be dob2_m
s1.select_by_visible_text('janvier')
Ignore formatting using mobile to answer.
Option 2
Use javascriptexecutor to scroll and click on element
ele= self.web_driver.find_element_by_xpath('SOME_XPATH')
coordinates = ele.location_once_scrolled_into_view
self.web_driver.execute_script('window.scrollTo({}, {});'.format(coordinates['x'], coordinates
You have to use ActionChains.
When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up.
More information.
Here is an example:
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
...#navigate to page
el=driver.find_element_by_id("bxFormPesquisa") #here I get the dropdown element
ActionChains(driver).click(el).perform()
select = Select(driver.find_element_by_name('bxFormPesquisa')) #again
select.select_by_visible_text('AL') #it is the text in dropdown list
#dropdown should be selected with value from line above

Categories

Resources