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')]
Related
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
I'm trying to find an account number in a table (it can be in many multiple tables) along with the status of the account. I'm trying to utilize find_element using the Xpath and the odd thing is that it is saying it cannot find it. You can see in the html that the id exists yet it is defaulting to my except saying table not found. My end result is to find the table that has the header Instance ID with the value of 9083495r3498q345 and to give the value under the Status field for that row in the same table. Please keep in mind that it may not be DataTables_Table_6 but could be DataTables_Table_i
<table class="data-table clear-both dataTable no-footer" cellspacing="0" id="DataTables_Table_6" role="grid">
<thead>
<tr role="row"><th style="text-align: left; width: 167.104px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Parent Instance ID</div></th><th style="text-align: left; width: 116.917px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Instance ID</div></th><th style="text-align: left; width: 97.1771px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Plan Name</div></th><th style="text-align: left; width: 168.719px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Client Defined Identifier</div></th><th style="text-align: left; width: 39.5729px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Units</div></th><th style="text-align: left; width: 89.8438px;" class="ui-state-default sorting_disabled" rowspan="1" colspan="1"><div class="DataTables_sort_wrapper"><span class="DataTables_sort_icon"></span>Status</div></th></tr>
</thead>
<tbody>
<tr role="row" class="odd">
<td style="text-align: left;"><span style="padding-left:px;\"><a href="#" class="doAccountsPanel" ></a></span></td>
<td style="text-align: left;"><span style="padding-left:px;\">Not Needed</span></td>
<td style="text-align: left;">The Product</td>
<td style="text-align: left;">9083495r3498q345</td>
<td style="text-align: left;">1</td>
<td style="text-align: left;">Suspended</td>
</tr></tbody></table>
try:
driver_chrom.find_element(By.XPATH,'//*[#id="DataTables_Table_6"]')
print("Found The Table")
except:
print("Didn't find the table")
I would have expected my print result to be "Found the Table", but I'm getting the "Didn't find the table".
DataTables are dynamic elements - the actual info they hold is being hydrated by javascript on an empty table skeleton, after page loads. Therefore, you need to wait for the table to fully load, then look up the information it holds:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
[...]
wait = WebDriverWait(driver, 20)
[...]
desired_info = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="DataTables_Table_6"]')))
print(desired_info.text)
See Selenium documentation here.
My code:
from selenium import webdriver
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
element = driver.find_element_by_id("Tabella")
time.sleep(5)
element.click()
time.sleep(5)
a=driver.find_element_by_id('rId_48').get_attribute('innerHTML')
print(a)
My output:
<td role="gridcell" style="" title="" aria-describedby="list_"><a title="Vedi su Google Maps" href="javascript:StatPortalOpenData.ODataUtility.openInStreetView(45.0760003999999,7.5911782);"><img alt="Vedi su Google Maps" height="25" width="25" style="vertical-align:middle" src="/sites/all/modules/spodata/metadata/viewer/multidimensional_viewer/img/streetView.png"></a></td>
<td role="gridcell" style="" class="" title="COLLEGNO" aria-describedby="list_Cccomune_608711150">COLLEGNO</td>
My desired output:
<td role="gridcell" style="" class="" title="COLLEGNO" aria-describedby="list_Cccomune_608711150">COLLEGNO</td><td role="gridcell" style="" class="" title="CITTA' METROPOLITANA DI TORINO" aria-describedby="list_Ccprovincia_1472723626">CITTA' METROPOLITANA DI TORINO</td>
So it is the second block of <td> </td>
Thank you!
If you want to target specific row and cell value you can use following CSS selector.
To print the element:
print(driver.find_element_by_css_selector("#rId_48>td:nth-child(2)").get_attribute('outerHTML'))
print(driver.find_element_by_css_selector("#rId_48>td:nth-child(3)").get_attribute('outerHTML'))
OR to print the text of the element
print(driver.find_element_by_css_selector("#rId_48>td:nth-child(2)").text)
print(driver.find_element_by_css_selector("#rId_48>td:nth-child(3)").text)
It looks like you selected the parent element of the <td> you want. Just use a css selector to get all the td elements inside it:
a=driver.find_elements_by_css_selector('#rId_48 td')
Note the "s" in find_elements.... This returns a list of all the <td> elements. So the one you want should be a[1].
I was trying to extract the data from the website, here:
https://apps.ecology.wa.gov/tcpwebreporting/reports/ust?CityZip=Seattle&County=King&StoredSubstance=Unleaded%20Gasoline
I click the > button to get more details of each gas station. I was trying to scrape the data, but I couldn't find a way to click > button using my codes.
I am able to extract each row's elements. what should I do next?
driver = webdriver.Chrome(executable_path=r'C:\Users\Owner\Desktop\Career\Coltura\chromedriver.exe')
driver.get('https://apps.ecology.wa.gov/tcpwebreporting/reports/ust?CityZip=Seattle&County=King&StoredSubstance=Unleaded%20Gasoline')
buttons = driver.find_elements_by_class_name(' details-control parent-td clickable parent-control')
driver.find_elements_by_tag_name('tr')
<tr class="clickable odd details" role="row">
<td class=" details-control parent-td clickable parent-control">
<button title="Toggle more information about the site RICK'S CHEVRON GROCERY" class="btn btn-sm btn-whitesmoke"></button>
</td>
<td class=" parent-td">27</td>
<td class=" parent-td">41179492</td>
<td class=" parent-td">A3602</td>
<td class=" parent-td">RICK'S CHEVRON GROCERY</td>
<td class=" parent-td">8506 5TH AVE NE</td>
<td class=" parent-td">Seattle</td>
<td class=" parent-td">98115</td>
<td class=" parent-td">King</td>
<td class=" parent-td">Northwest</td>
</tr>
To locate element with multiple class name, you can use *_by_css_selector not _by_class_name.
I suggest to use method : .location_once_scrolled_into_view before click the element.
This is for click each arrow button you mean:
driver.get('https://apps.ecology.wa.gov/tcpwebreporting/reports/ust?CityZip=Seattle&County=King&StoredSubstance=Unleaded%20Gasoline')
#add some wait here.....
arrows = driver.find_elements_by_css_selector('td[class*="details-control"]')
for arrow in arrows:
arrow.location_once_scrolled_into_view
time.sleep(0.5)
arrow.click()
I'm using Ghost for Python 2.7 and I'm trying to click in a link which is in a table. The problem is that I have no ID, name... This is the HTML code:
<table id="table_webbookmarkline_2" cellpadding="4" cellspacing="0" border="0" width="100%">
<tr valign="top">
<td>
<a href="/dana/home/launch.cgi?url=.ahuvs%3A%2F%2Fhq0l5458452ERA-w-Xz8G3LKe8JNM%2F.ISDXWXaWXUivecOc" target="_blank" onClick='javascript:openBookmark(
this.href, "yes", "yes");
return false;' ><img src="/dana-cached/imgs/icn18x18WebBookmarkPop.gif" alt="This will open in a new TAB" width="18" height="18" border="0" ></a>
</td>
<td width="100%" align="left">
<a href="/dana/home/launch.cgi?url=.ahuvs%3A%2F%2Fhq0l5458452ERA-w-Xz8G3LKe8JNM%2F.ISDXWXaWXUivecOc" target="_blank" onClick='JavaScript:openBookmark(
this.href, "yes", "yes");
return false;' ><b>**LINK WHERE I WANT TO CLICK**</b> </a><br><span class="cssSmall"></span>
</td>
</tr>
</table>
How can I click in this kind of link ?
Seems like Ghost's Session.click() takes a CSS selector. Here only the table has an ID, so a selector that takes the second td that is a descendant of that ID and finds the a element should work:
session.click('#table_webbookmarkline_2 td:nth-child(2) a')