Finding elements in Selenium - python

My button looks like this:
<button type="button" data-toggle="modal" data-target="#modal-order-export" data-backdrop="false" class="btn btn-default btn-sm">
<i class="fa fa-file-excel-o"></i> 엑셀 저장
</button>`
How can I click this object via selenium to automate some work? I tried:
driver.find_element_by_link_text('엑셀 저장').click()

You could take its xpath by right-clicking the element in developer tools and choosing copy. Then just find the element using xpath.

The WebElement which you are trying to search/find is within <button> tag hence find_element_by_link_text may not work. We will be using an xpath to locate the WebElement as follows:
driver.find_element_by_xpath("//button[#class='btn btn-default btn-sm']/i[#class='fa fa-file-excel-o']").click()

Related

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

Finding a button by the span text in python selenium

<li id="b43d980c-53bb-4892-8f32-5ae8ebd9026e" class="">
<input type="button">
<span>Male</span>
</li>
How can I locate this and then click on it?
Text() attribute in xpath locator should do the trick to find the span:
driver.find_element(By.XPATH("//span[text()='Male']"))
Want to grab the input before?
driver.find_element(By.XPATH("//span[text()='Male']/preceding-sibling::input"))

Web element not found but appears when inspected manually

I am unable to access this menu in selenium the web element doesn't appear in inspector until manually done
<a id="cke_3275" class="cke_menubutton cke_menubutton__table cke_menubutton_off cke_menubutton__table" href="javascript:void('Table Properties')" title="Table Properties" tabindex="-1" _cke_focus="1" hidefocus="true" role="menuitem" aria-haspopup="false" aria-disabled="false" onmouseover="CKEDITOR.tools.callFunction(666,5);" onmouseout="CKEDITOR.tools.callFunction(667,5);" onclick="CKEDITOR.tools.callFunction(668,5); return false;">
<span class="cke_menubutton_inner">
<span class="cke_menubutton_icon">
<span class="cke_button_icon cke_button__table_icon" style="background-image:url(https://lms.testbook.com/vendor/ckeditor/plugins/icons.png?t=E6FD);background-position:0 -1896px;background-size:auto;">
</span>
</span>
<span class="cke_menubutton_label">
Table Properties
</span>
</span>
</a>
I tried accessing parent, click and actions.perform() nothing seems to work.
When i hover over the menu contents i see javascript:void('contentname'), i pasted this in the inspector and found the web element.
iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/iframe")))
driver.switch_to.frame(iframe)
driver.find_element_by_tagname("a")
switch to iframe and then add rest of the code
driver.switch_to.default_content()
you need to switch to default content after you are done with the element and want to interact with an element outside the iframe

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']")

how to press button with same id but in different class?

I am trying to use selenium webdriver to click on a button in a different class. The webpage is as follows:
<div class="fade tab-pane" id="mm9-tab-content" role="tabpanel">
<div class="button-submit">
<button class="btn btn-primary btn-lg btn-block post" id="download" type="button">Download</button>
</div>
</div>
<div class="fade tab-pane" id="dm3-tab-content" role="tabpanel">
<div class="button-submit">
<button class="btn btn-primary btn-lg btn-block post" id="download" type="button">Download</button>
</div>
</div>
the data is in 2 classes mm9-tab-content and dm3-tab-content. I want the system to click on the download button in the dm3-tab-content.
I tried using
driver.find_element_by_xpath('xpath = (//*#id="download")[1]').click()
to get the second instance of download but it doesn't seem to work. Any ideas?
Your XPath seem to be invalid. Try below instead
driver.find_element_by_xpath('(//*[#id="download"])[2]').click()
Note that, unlike in Python, in XPath nodes indexation starts from 1, so the second element should have index [2]
If you know the order in which the buttons appear and which one you want to use, you could find all the elements with the id="download", using find_elements_by_css_selector:
buttons = driver.find_elements_by_css_selector('#download')
Then, you can access each button in order.
To click the button under mm9-tab-content class use
driver.find_element_by_css_selector('#mm9-tab-content #download').click()
To click the button under dm3-tab-content class use
driver.find_element_by_css_selector('#dm3-tab-content #download').click()

Categories

Resources