Unable to click on javascript element through selenium driver - python

I am using given below xpath but my script is not clicking on these element (python, scrapy, selenium)
driver.find_element(By.XPATH, '//*[#id="MiniReport2"]/thead/tr/td[#class="Over" and contains(#onclick,"MenuClick")]')
HTML Sample:
<table id="MiniReport2" border="0" cellspacing="0" cellpadding="0" class="Menu Unit" style="">
<thead>
<tr>
<td onmouseover="className='Over'" onmouseout="className=''" onclick="javascript:MenuClick('MiniReport2');" class="">
<div class="Plus">4. Report</div>
</td>
</tr>
</thead>
</table>

Welcome to SO.
Here is the xpath that you can use.
//table[#id='MiniReport2']//td[contains(#onclick,'MenuClick')]
Here is the script (I guess you are using python)
driver.find_element_by_xpath("//table[#id='MiniReport2']//td[contains(#onclick,'MenuClick')]").click()
If you want to click on 4. Report then use the below
driver.find_element_by_xpath("//table[#id='MiniReport2']//td[contains(#onclick,'MenuClick')]/div[normalize-space(.)='4. Report']").click()

To handle dynamic element use WebdriverWait and following locator strategy.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,'//table[#class="Menu Unit"][starts-with(#id,"MiniReport")]//tr//td[contains(#onclick,"MenuClick")]/div[#class="Plus"][contains(.,"4. Report")]')))
element.click()

Related

Selenium unable to click() on "btn3d"

I'm using Selenium in Python to download a file as a bot. However, I am facing problems clicking in a button... When I click the "Búsqueda Avanzada" (advanced search) button manually to deploy a bar for a query, it goes ok, but through Selenium a white page appears. I guess the my problem is that I'm trying to click a 3D button which can't be done in a simple way as other buttons.
The simplified HTML code from the webpage would be:
<table cellpadding="0" cellspacing="0" class="Toolbar">
<tbody>
<tr>
<td nowrap="" class="TBGroup TBGroup2">
<a class="advancedsearch btn btn3d tbbtn" href="javascript:" style="position:static" arwindowid="3">
<div id="TBadvancedsearch">Búsqueda avanzada</div>
</a>
</td>
</tr>
</tbody>
</table>
The summarized script I've been trying to go with, is the following:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
action1 = ActionChains(driver)
element = driver.find_element(By.CSS_SELECTOR, '#Toolbar > table > tbody > tr > td.TBGroup.TBGroup2 > a.advancedsearch.btn.btn3d.tbbtn')
action1.move_to_element(element)
btn_adv_search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'TBadvancedsearch')))
action1.move_to_element(btn_adv_search)
action1.click()
action1.perform()
But what should be the deployment of a query bar, turns out to pop a TimeoutException or refresh as a kind of a blank page?

Unable to click label element using Selenium + Python

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()

Unable to locate element and no such element error using Selenium and Python

I want to click a button Customer Details, but i got an error. This is an error from python :
Message: no such element: Unable to locate element
I tried a few code(listed below) but they didn't work. Any ideas?
1. driver.find_element_by_xpath("(//a[contains(text(),'Customer Details')])[11]").click()
2. driver.find_element_by_xpath("(//a[#href='https://mylink' and #class=' class="btn-sm bg-navy btn-default"']").click()
3. driver.find_element_by_link_text("Customer Details").click()
An this is my HTML Code:
<table class="table table-bordered table-striped dataTable no-footer DTFC_Cloned" style="width: 100%; padding: 0px; margin: 0px;" role="grid" aria-describedby="tbl_so_info">
<thead>
<tr role="row" style="height: 0px;">
<th class="sorting" tabindex="0" aria-controls="tbl_so" rowspan="1" colspan="1" aria-label=": activate to sort column ascending"></th>
<th class="sorting_desc" tabindex="0" aria-controls="tbl_so" rowspan="1" colspan="1" aria-label="Customer No.: activate to sort column ascending" aria-sort="descending"></th>
</tr>
</thead>
<tbody>
<tr role="row" class="odd" data-dt-row="0" style="height: 38px;">
<td data-dt-row="0" data-dt-column="0">
Customer Details
Create Ticket
</td>
</tr>
</tbody>
</table>
Using WebDriverWait wait for element to be clickable before click on it:
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, 10)
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Customer Details'))).click()
# css selector
# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="https://mylink"]'))).click()
To click() on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Customer Details"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn-sm.bg-navy.btn-default[href='https://mylink']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn-sm bg-navy btn-default' and #href='https://mylink'][contains(.,'Customer Details')]"))).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
Reference
You can find a relevant detailed discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
The xpath written by you was correct until u added [11].
Now your code is searching the a tag with Customer Details. But adding [11] will lead it to searching such element for 11th result which is not present in your code.
Hence its says no such element found.
Try writing only the below code and it would work fine.
xpath = " //a[contains(text(),'Customer Details')] "
NOTE:- NEVER USE these ([1] [11] [2]) things in your locators it is not a good approach as if the structure of the program changes then the locator might not work.

How to locate element without id,name and xpath in webdriver selenium google chrome

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

How do i click a Button inside a Table using Selenium(Python)

<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()

Categories