I've been trying to select one image tag, however, since it doesn't have any name and id I'm having trouble while doing the same.
HTML:
<tr __gwt_row="0" __gwt_subrow="0" class="GPBYFDECG">
<td class="GPBYFDEBG GPBYFDEDG GPBYFDEEG datagridCellStyle">
<div style="outline-style:none;" __gwt_cell="cell-gwt-uid-29">
ACD DETAILS NEW
</div>
</td>
<td class="GPBYFDEBG GPBYFDEDG datagridCellStyle">
<div style="outline-style:none;" __gwt_cell="cell-gwt-uid-30">
ACD Details
</div>
</td>
<td class="GPBYFDEBG GPBYFDEDG GPBYFDEOG datagridCellStyle">
<div style="outline-style:none;" __gwt_cell="cell-gwt-uid-31" tabindex="0">
//IMAGE THAT I NEED TO SELECT AND CLICK
<img onload="this.__gwtLastUnhandledEvent="load";" src="http://172.00.00.00:8080/demoreports/DemoReportsApp/clear.cache.gif" style="width:25px;height:23px;background:url(http://172.00.00.00:8080/demoreport/DemoReportsApp/0210CFCB6CBE82D7E9FAC82D9F901495.cache.png) no-repeat -333px 0px;" border="0">
</div>
</td>
Code:
driver.find_element_by_xpath('//img[#onload="this.__gwtLastUnhandledEvent="load";"]').click()
Above code doesn't work.
Is there any other way, I can click() on this img as it generates javscript rendered reports.
The desired element is a dynamic element so you have to induce WebDriverWait for the desired ElementToBeClickable and you can use either of the following Locator Strategies as solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.datagridCellStyle img[onload*='gwtLastUnhandledEvent'][src*='demoreports/DemoReportsApp/clear']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[contains(#class, 'datagridCellStyle')]//img[contains(#onload, 'gwtLastUnhandledEvent') and contains(#src, 'demoreports/DemoReportsApp/clear')]"))).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
I have this line of code that I want to click using Selenium WebDriver with Python, it is derived from an iframe:
<table width="100%" cellspacing="0" align="center" class="maintable">
<tr>
<td align="left" style="white-space: nowrap;">
<label for="file" class="pb default" style="display: inline-block;margin: 5px 10px;">Select File</label>
<input id="fileName" onmousedown="" type="text" value="" class="fld fld_ro text ib" readonly size="50"/>
<input id="file" type="file" name="value" title="Specify a New File" onchange="" size="35" class="text" style=" width: 0.1px;height: 0.1px !important;fileStyle: 0;overflow: hidden;position: absolute;z-index: -1;opacity: 0;" value="" onclick="if(!parent.undef(parent.firingControl) && parent.firingControl.id==this.id){parent.sendEvent('clientonly','clickFileButton', this.id)}">
</td>
</tr>
</table>
I want to click the "Select File" label (it is a button but without XPath or id) but I really am unable to do so...
this is the iframe :
<iframe id="upload_iframe" allowtransparency="true" class=" " role="presentation" tabindex="0" src="http://www.test.com/webclient/utility/uploadfile.jsp?controlId=itemimage_3_1&componentId=itemimage_3_1-if&uisessionid=1689" scrolling="no" marginheight="0" marginwidth="0" onfocus="setCurrentfocusId(event,this);" style="border:0px;" width="400" height="33" frameborder="0"> </iframe>
I have used this line of script to click the label but the button does not respond to my code:
button_element = browser.find_element_by_class_name('pb default')
button_element.click()
Do you know how to click that element? I am using firefox to do that... Any answer is appreciated, thx.
Try to use explicit wait before click action:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it(
(By.ID, "upload_iframe")))
browser.switch_to.frame(browser.find_element_by_id("upload_iframe"))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".pb.default"))).click()
To switch back to the original content use the following:
browser.switch_to.default_content()
I hope it helps you!
Get the element using the inner text of the lable,
driver.findElement(By.xpath("//label[contains(text(),'Select File')]")).click()
To access an iframe you have to switch to it with .switchTo().frame. See below. I commented out the switch back command.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser, 10).until(EC.presence_of_element_located(
(By.ID, ".pb.upload_iframe")))
browser.switchTo().frame("upload_iframe")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".pb.default"))).click()
#browser.switchTo().defaultContent()
I want to get some td data from dynamic table with selenium and push them in array.
I tried to use:
driver.find_elements_by_class_name("row_data")
and get the html then find td but the list element can't get attribute innerHTML ...
<tr class="row_data text-silver">
<td class="link">
<a href="/Account/UserCompleteRegister" data-toggle="tooltip" data-placement="left" title="ویرایش" class="btn btn-info btn-xs btnEditUser">
<i class="fa fa-edit"></i>
</a>
مرضیه<input type="hidden" value="1332162477" class="userId">
</td>
<td class="text-right">
ایرج ساعی
</td>
<td class="text-right">
6180033005
</td>
<td class="text-right">
</td>
<td class="text-right">
25 سال و 2 روز
</td>
<td class="text-right">
<span class="GenderText">زن</span>
</td>
</tr>
A bit of more information with respect to which data you are exactly looking for would have helped us to construct the answer in a canonical way. However as the class attribute of the <tr> node contains row_data and text-silver you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "tr.row_data.text-silver))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//tr[#class='row_data text-silver']"))).text)
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
If its single table on your page and you want to use xpath then please refer below solution :
1. //table//td[*]
2. //table//tr//td[*]
or else provide specific table id to handle your tabel
My td contains
<tbody id="gridview-1161-body">
<tr id="gridview-1161-record-19832230" data-boundview="gridview-1161" data-recordid="19832230" data-recordindex="2" class="x-grid-row x-grid-data-row" tabindex="-1">
#supplier item
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1154" id="ext-gen2524">
<div class="x-grid-cell-inner " style="text-align:left;">
<div class="rp-invalid-cell rp-icon-alert-require-field"></div>xxx/div></td>
#id
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1156" id="ext-gen2526">
<div class="x-grid-cell-inner " style="text-align:left;">
<div class="rp-invalid-cell rp-icon-alert-require-field"></div>yy</div></td>
#cost
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1157" id="ext-gen2527">
<div class="x-grid-cell-inner " style="text-align:right;">
<div class="rp-invalid-cell rp-icon-alert-require-field"></div>$15.00</div></td>
#qty - **here i want to set value 10** id is dynamically generating
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1158 rp-grid-editable-cell rp-grid-editable-cell" id="ext-gen2528">
<div class="x-grid-cell-inner " style="text-align:right;">
<div class="rp-invalid-cell rp-icon-alert-require-field" id="ext-gen2632"></div> </div></td>
</tr><tbody>
i want to click this td and set value as 10 then click enter.I have tried.
e=driver.find_element_by_class_name('x-grid-cell x-grid-td x-grid-cell-headerId-gridcolumn-1158 rp-
grid-editable-cell rp-grid-editable-cell').send_keys('10')
e.send_keys(Keys.ENTER)
and
e=driver.find_element_by_xpath("//div[#class='x-grid-cell x-grid-td x-grid-
cell-headerId-gridcolumn-1158 rp-grid-editable-cell rp-grid-editable-
cell']")
e.send_keys("10")
but im getting
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element: {"method":"css
selector","selector":".x-grid-cell x-grid-td
x-grid-cell-headerId-gridcolumn-1158 rp-grid-editable-cell
rp-grid-editable-cell"}
Try
e = driver.find_element_by_css_selector('td.x-grid-cell.x-grid-td.x-grid-cell-headerId-gridcolumn-1158.rp-grid-editable-cell.rp-grid-editable-cell')
e.send_keys('10')
e.send_keys(Keys.ENTER)
Plus, i think that this kind of error NoSuchElementException appears when Selenium is unable to find your element. The reasons may be:
Your driver.find_element has the wrong parameters, but considering your code, it seems unlikely.
The element you try to find is not displayed. You should consider using the wait:
WebDriverWait(self.driver, time).until(EC.visibility_of_element_located((By.CLASS_NAME, class_name)))
To click the <td> element, set value as 10 then to click() Enter you have to to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.x-grid-cell.x-grid-td.rp-grid-editable-cell[role='gridcell']")))
element.click()
element.clear()
element.send_keys('10')
element.(Keys.ENTER)
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[#role='gridcell' and contains(#class, 'rp-grid-editable-cell')]")))
element.click()
element.clear()
element.send_keys('10')
element.(Keys.ENTER)
Note : You have to add the following imports :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
how can i locate element td without name , id attributes?
<tr class="odd" onmouseover="this.className='highlight'" onmouseout="this.className='odd'">
<td style="width:4%;">J199</td>
<td style="width:5%;">056962840</td>
<td style="width:3%;">S</td>
<td style="width:5%;">11</td>
<td style="width:7%;">0606353</td>
<td style="width:7%;">4846962</td>
<td style="width:3%;">1</td>
<td style="width:20%;">S1-4100181163-MANUAL</td>
<td style="width:5%;">2019-07-03</td>
<td style="width:5%;"> </td>
<td style="width:5%;cursor: pointer;"><span title="CSARL-SB/SINGAPORE PARTS DC">Y850</span></td>
<td style="width:5%;">2019-09-04</td>
<td> </td>
</tr>
i only want to locate
<td style="width:5%;">2019-09-04</td>
and call it in python webdriver
To locate the <td> with text as 2019-09-04 you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using xpath and element position:
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "//tr[#class='odd' and contains(#onmouseover, 'highlight')]//following-sibling::td[12]")))
Using xpath and title CSARL-SB/SINGAPORE PARTS DC:
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#title, 'CSARL-SB/SINGAPORE PARTS DC')]//following::td[1]")))
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
If you want to match <td> tag having the text of 2019-09-04 it would be something like:
//td[text()='2019-09-04']
If you want to match the <td> tag which is before the last <td> tag go for position() and last() functions combination:
//td[position()=last()-1]
More information: XPath Operators & Functions
<table _ngcontent-c19="" class="table">
<tbody _ngcontent-c19="">
<tr _ngcontent-c19=""><!---->
<td _ngcontent-c19="" class="ng-star-inserted">
<div _ngcontent-c19="" class="span3" style="height: auto;">
<div _ngcontent-c19="" class="text-center" style="visibility: visible;">
<button _ngcontent-c19="" class="b1" type="submit">Click This</button>
</div><!---->
</div>
</td>
</tr>
</tbody>
</table>
I'm trying to click a button inside a table using SELENIUM.
The Code I've Written is
driver.find_element_by_xpath("//*[contains(getText(),'Click This')]").click()
and
driver.find_element_by_class_name('b1').click()
they both throw an Element not Found Exception
you can try with this xpath :
wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Click This') and #class='b1']"))).click()
Note that you will have to import :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Because your code runs faster than your browser that's why you need to tell him to wait until the element is visible and clickable.
button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[text()='Click This']"))
button.click()