How do I tick a checkbox in a table and modal window? - python

On a website, I need to click a checkbox in a modal window that pops up.
I can't seem to get the proper xpath for it.
I've tried:
req_checkbox = driver.find_element_by_xpath("//td/div/input[#data-id='34970']")
driver.execute_script("arguments[0].click();", req_checkbox)
Also tried:
req_checkbox = WebDriverWait(usedriver, 15).until(EC.presence_of_element_located((By.XPATH,'//div[#class="grid-check-box"]/input[#data-id="34970" and type="checkbox"]')))
The data-id is what would make that checkbox unique as there is more than one checkbox with the same class. Here is the code snippet:
<td role="gridcell" id="AddFromFirmMasterListGrid_active_cell" class="">
<div class="grid-check-box">
<input class="LinkServiceContactToEnvelopeMap" data-id="34970" data-idtoken="5F3244E8BF5E488D14E28C05BE2DD2C524B5E9F0" data-envelopeidtoken="0A1257B11E29C56650A270E56FD12007CDF2F917" type="checkbox">
<span></span>
</div>
</td>
How do I solve this problem?

You can try with this css selector :
div.grid-check-box input.LinkServiceContactToEnvelopeMap[type='checkbox']
Xpath would be :
//div[#class='grid-check-box']/input[#class='LinkServiceContactToEnvelopeMap' and #type='checkbox']
First check that in DOM, if there are multiple enteries then you will have to make necessary changes.
Let me know, if you have any more concerns.

Related

Python, Selenium: How to get text next to element

I'm fairly new to selenium and I'm trying to get the text of a cell next to a known element.
This is an excerpt of a webtable:
<div class="row">
<div class="cell">
text-to-copy
</div>
<div class="cell">
<input type="text" size="10" id="known_id" onchange="update(this.id);" onclick="setElementId(this.id);"/>
X
</div>
<div class="cell right">
<div id="some_id">?</div>
</div>
</div>
It looks something like this:
From this table I would like to get the text-to-copy with selenium. As the composition of the table can vary, there is no way to know that cells xpath. Therefore I can not use selenium_driver.find_element_by_xpath(). The only known thing is the id of the cell next to it (id=known_id).
The following pseudo code is to illustrate what I'm looking for:
element = selenium_driver.find_element_by_id("known_id")
result = element.get_visible_text_from_cell_before_element()
Is there a way to get the visible text (text-to-copy) with selenium?
I believe you can fairly use xpath, all other locators that Selenium supports would not work, becasue we have to traverse upward in DOM.
The below xpath is dependent on known_id
//input[contains(#id,'known_id')]/../preceding-sibling::div
You have to either use .text or .get_attribute etc to get the text.
Sample code :
time.sleep(5)
element = selenium_driver.find_element_by_xpath("//input[contains(#id,'known_id')]/../preceding-sibling::div").get_attribute('innerText')
print(element)

Python / Selenium - access not interactive element

What I need:
Select one of the options from the drop-down list
Only BUTTON element is visible (SELECT element is not visible)
This is html code:
<td data-bind="css: { 'cell-error': applicantNames.hasError }, attr: { title: applicantNames.message }" title="">
<select data-bind="options: $parent.applicants, multivalue: applicantNames" multiple="multiple" style="display: none;">
<option value="ABC">ABC</option>
<option value="XYZ">XYZ</option>
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 236px;"><span class="ui-icon ui-icon-triangle-2-n-s"></span><span>Please select...</span></button>
</td>
This is how it looks like:
The problem is that I would like to select one of these options by using Selenium, but because the SELECT element is not interactable/not visible ... not sure what to do...
If you will choose an option from the drop-down the BUTTON element will be updated like this
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all ui-state-active" aria-haspopup="true" style="width: 236px;">
<span class="ui-icon ui-icon-triangle-2-n-s"></span><span>ABC, XYZ</span>
</button>
Even if I will update the BUTTON section manually, it will still not update the element visually..... so I'm a bit stuck to find a way how to interact with these elements by using Selenium...
P.S. I'm looking for ANY headless methods, e.g. Selenium / JavaScript execution through selenium and e.t.c...
See the problem is that "This is not built using Select - option tag", so one can not directly make use of Select class from Selenium support package.
Instead you can try to click on that menu using Selenium .click()
in your case something like this
driver.find_element_by_xpath("//span[contains(text(), 'Please select')]").click()
and then store all the visible option in a list of web elements using find_elements and then iterate the list :
for option in list_of_options:
if option.text == "your desired option"
option.click()

Selenium Can't find "HOME" element on Facebook

I'm doing some exercises with selenium, trying to scrape a few pages on facebook.
However, I'm not able to find the "Home" button link element to keep going. This on the left menu in any user's profile.
From what I'm seeing in the page code, the link is here:
<div class="_2yaa" data-key="tab_home">
<a class="_2yau" data-endpoint="/seudogshow/?ref=page_internal" href="/seudogshow/?ref=page_internal">
<span class="_2yav">Home</span>
<span class="img _55ym _55yn _55yo _2wwb" aria-busy="true" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuetext="Loading..."></span>
</a>
</div>
How would you guys go about clicking this button?
I tried with something like this:
driver.find_element_by_xpath("//a[contains(text(), 'Home')]").click()
or
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'Home')]"))).click()
But I'm clearly doing it wrong.
The element you are trying to click has a span tag, not a. Try the following:
driver.find_element_by_xpath("//span[contains(text(), 'Home')]").click()

find button that is within 3 divs selenium python

I am trying to find the text of a button. here is the html layout.
<div id="fulfillment-add-to-cart-button">
<div class="fulfillment">
<div>
<div style="position:relative">
<button class="btn ban-disabled"> Sold Out </button>
</div>
</div>
</div>
</div>
Here is what I have
driver.find_elements_by_xpath("//div[#id='fulfillment-add-to-cart-button']/div/div/div/button[0]")
I keep returning a empty array. I've tried also finding the button with the button classname however that also returns an empty array.
The button is a single element, correct? So you would find by element, not elements.
Try this, to search for that specific text within that button
button_elem = driver.find_element_by_xpath("//div[#id='fulfillment-add-to-cart-button']//button")
print(button_elem.text)
If that doesn't work we may need to see more of the HTML that surrounds that section

ElementNotVisibleException: element not visible Python

I'm new to Python and Selenium and I've come across an issue when trying to click on a button via webdriver.
The HTML of the div I'm trying to click through is:
<div class="">
<form method="POST">
<input class="hide" id="accept" name="accept" type="text" value="yes" readonly="">
<a href="/" class="btn btn-red">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink=" " xlink:href=""></use>
</svg>
</div>
Decline
</a>
<button class="btn btn-green" type="submit">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink="" xlink:href=""></use>
</svg>
</div>
Accept
</button>
</form>
</div>
I want to use .click() on the button with class name 'class="btn btn-green"'.
I have used the following code to select the element (after following the solutions in other similar SO questions.
driver.find_element_by_css_selector(".btn-green")
It looks like webdriver can find the element but when I try to apply .click() I get:
ElementNotVisibleException: element not visible
I then did some digging into the element (thinking I could bypass by using x, y coordinates to click through) but after using .size and .location I get:
{'x': 0, 'y': 0}
{'height': 0, 'width': 0}
Any help you could give on how to get around this would be really appreciated.
Thanks
The error hints to the potential problem, selenium does find it but it is not visible so you cannot click on it. You could check for visibility with is_displayed before clicking on it. Another possibility is if there is another button with that class that is hidden, try to see if you can select a visible element in the browser console (Control+Shift+J) with $(".btn-green")
Some xpaths that might work that are more robust for your problem:
//button[#type="submit" and text()="Accept"]
//button[#type="submit" and contains(#class, "btn-green")]
To click on the button with text as Accept you can use the following line of code :
driver.find_element_by_css_selector("button.btn.btn-green[type='submit']")

Categories

Resources