Python selenium checkbox click does not work - python

Hey im using python selenium to download data from table. But when im want to prepare data i cant select a checkbox 'toggle all'....
im geting on page:
browser.get("https://gold.jgi.doe.gov/studies?setColumns=yes&Organism.NCBI+Taxonomy+ID=%3D500633")
click 'select columns for table'
browser.find_element_by_xpath('//*[#id="showColsButton"]').click()
and here we are checkbox ;toggle all'...
browser.find_element_by_xpath('//*[#id="selectFieldsList"]/thead/tr[2]/td/input').click()
Realy tryed xpath, css selector....
and here we are html fragment:
<table class="selectFieldsList" id="selectFieldsList">
<thead>
<tr><td colspan="2" align="center">
Select Fields using the Checkboxes<br>
<input type="submit" value="Submit" name="fieldSubmit" id="submitMe" class="submitMe">
</td></tr>
<!-- add a select all option -->
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('selectField');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
<tr><td> <input type="checkbox" onclick="toggle(this)"> Toggle All<br> </td></tr>
<tr><td>* = required column</td><td> </td> </tr>
<tr><td> <input type="button" id="entityFieldSelectorToggle" value="Expand All Fields"> </td>
</tr></thead>
<tbody>

Instead of using sleep use explicit wait is good practise, because it will same 5 sec of time even after element is found. Hope it will be useful to you
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://gold.jgi.doe.gov/studies?setColumns=yes&Organism.NCBI+Taxonomy+ID=%3D500633")
driver.find_element_by_xpath('//*[#id="showColsButton"]').click()
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="selectFieldsList"]/thead/tr[2]/td/input')))
driver.find_element_by_xpath('//*[#id="selectFieldsList"]/thead/tr[2]/td/input').click()

Related

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

How to get data of html table with selenium python

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

how to select date in jquery date time picker in selenium python

I want to select the date and time from jquery date time picker which is present inside the iframe. I have switched to frame and clicked on the field which opens the date time picker but i want to send value from my code like send keys,but I'm not able to send. I'm doing this in python.
Here is my html code:
<div class="add_folder">
<form method="POST" action="some action url " id="add-attendance">
<table class="table_blue" >
<tr>
<td> <label for="start_time">Start Time</label></td>
<td><input type="text" value="" name="start_time" id="start_time"/></td>
</tr>
<tr>
<td> <label for="end_time">End Time</label></td>
<td> <input type="text" value="" name="end_time" id="end_time"/> </td>
</tr>
<tr>
I did this using xpath but it didn't worked for me. Here is my selenium code to send keys:
driver.find_element_by_xpath('[#id="unowacalendar"]/div/div/table/tbody/tr[2]/td[5]').send_keys("2019-12-03")
Try targeting the input elements. For example:
driver.find_element_by_id('start_time').send_keys('yourValue')
You may need a wait e.g.
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID , 'start_time'))).send_keys('yourValue')
Additional imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

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

Selenium Source Missing Login Fields

I'm trying to use Selenium with Python to log into an ESPN page:
http://games.espn.go.com/ffl/scoreboard?leagueId=34467&matchupPeriodId=1
When you go to this page it pops up a login box but Selenium can't seem to find the fields for it.
Here's my code:
driver = webdriver.Chrome()
driver.get('http://games.espn.go.com/ffl/scoreboard leagueId=34467&matchupPeriodId=1')
username = driver.find_element_by_name('username')
username.send_keys('XXXXX')
password = driver.find_element_by_name('password')
password.send_keys('XXXXX')
submit_button = driver.find_element_by_name('submit')
submit_button.click()
When I view the page source in Chrome myself I see the form:
<table width=100% border=0 cellpadding=0 cellspacing=0 class="bodyCopy"><tr>
<td width=15%><b>MEMBER NAME:</b></td>
<td width=1%> </td>
<td><input name="username" size="16" maxlength="64" value="" class="select"> </td>
</tr>
<tr>
<td><b>PASSWORD:</b></td>
<td> </td>
<td><input type="password" name="password" size="16" maxlength="25" value="" class="select"></td>
</tr>
<tr>
<td colspan=2> </td>
<td><input type="submit" name="submit" value="Log In" class="select"></td>
</tr>
</form>
</table>
However, when I use...
page = driver.page_source
...the form is missing entirely. I can see the same form in the browser window that Selenium opens up though, it's just missing from the source. Any ideas?
The first problem is that the login form is inside an iframe and you need to switch to it:
driver.switch_to.frame("disneyid-iframe")
And, the second issue is that the form is loaded dynamically. You need to explicitly wait for the username field to show up before starting to log in (well, as a human would do):
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.visibility_of_element_located((By.CSS_SELECTOR, "div.field-username-email input")))
print(driver.page_source)
Note that your locators point to non-existing elements. Use instead:
username = driver.find_element_by_css_selector("div.field-username-email input")
password = driver.find_element_by_css_selector("div.field-password input")
submit_button = driver.find_element_by_xpath("//button[contains(., 'Log In')]")

Categories