How to Select Div within a Div with index in Xpath Syntax? - python

<div class="j-C j-C-yj" style="max-height: none; -moz-user-select: none; visibility: visible; left: 218px; top: 105px; display: none;" role="menu" aria-haspopup="true">
<div id=":1r" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
<div id=":1u" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
</div>
so far i am creating xpath to select element with id=":1r" is
"(//div[contains(#class,'j-C') and contains(#class ,'j-C-yj')]/div)[1])"
i had also tried
"(//div[contains(#class,'j-C') and contains(#class ,'j-C-yj')]/div)[1])"
but none is working please help !
P.S : I cannot find element with id as id of the page is created dynamically

Just use xpath('.//div[contains(#class, "j-C") and contains(#class, "j-C-yj")]') as another answer already showed you.
Another Updates:
As OP kept changing the constraints of the question, here is the full solution does exactly what OP eants.
Sample: I'm using lxml to parse your string and do the xpath
from lxml import etree
s = '''<div class="j-C j-C-yj" style="max-height: none; -moz-user-select: none; visibility: visible; left: 218px; top: 105px; display: none;" role="menu" aria-haspopup="true">
...: <div id=":1r" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
...: <div id=":1u" class="j-qn" style="-moz-user-select: none;" role="separator"></div>
...: </div>'''
# I need to wrap your string with <root> element otherwise first div will become the root
tree = etree.fromstring('<root>'+s+'</root>')
# xpath always returns a list, so just loop through the list and the first element is what you want
for node in tree.xpath('.//div[contains(#class, "j-C") and contains(#class, "j-C-yj")]'):
print etree.tostring(node[0])
<div id=":1r" class="j-qn" style="-moz-user-select: none;" role="separator"/>

Seems like there is a problem in your xpath towards the end. Try this piece of code:
driver.find_element_by_xpath("//div[contains(#class,'j-C') and contains(#class ,'j-C-yj')]/div[1]")

Related

Trying to select a check box but not getting selected

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-imageModel" style="display: block; z-index: 1002; outline: 0px; height: auto; width: 700px; top: 225.5px; left: 278.5px;"><div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix"><span class="ui-dialog-title" id="ui-dialog-title-imageModel">ATTENTION TAX PAYERS!!!!!</span><span class="ui-icon ui-icon-closethick">close</span></div><div id="imageModel" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; height: 92.4px;" scrolltop="0" scrollleft="0">
<ul><li class="redFont"> Please insist on getting Form 16/16A from your Deductor downloaded only from Traces.Valid form 16/16A.<i><u> click here.</u></i></li></ul>
<div class="floatLeft margintop20"><input type="checkbox" id="Details" name="Details" onclick="checkModal('modalPagee')"> I agree to the usage and acceptance of Form 16 / 16A generated from TRACES</div>
<div class="floatLeft margintop20">
<input value="Proceed" type="button" class="button" id="btn" disabled="disabled" onclick="goCancel()">
</div>
</div></div>
This is the HTML code on which I am working, Here I want to select the input element of type checkbox, when I was trying it as
check_box = find_element_by_id("Details").click() I was failing to select it .
if need of more details in
"https://www.incometax.gov.in/iec/foportal" please visit this site,
when after login, goto -> e-file -> income_tax_returns -> view for 26as and continue, Then we will get another window opened and will get a popup here in pop up we need to select the checkbox.
That details is in new tab, you need to switch the focus like below :
wait = WebDriverWait(driver, 10)
new_handle = driver.window_handles
print(len(new_handle))
driver.switch_to.window(new_handle[1])
wait.until(EC.element_to_be_clickable((By.ID, "Details"))).click()
Try:
check_box = find_element_by_xpath('//*[#id="Details"]').click()

Select dropdown box option using Selenium python. ng select combobox XPath

Using python and selenium to select an option in a combobox drop down ng select. I want to select the 'MALE' option in a dropdown select. I am trying to use XPath.
The code I use:
driver.find_element_by_id("sex_0").click()
driver.find_element_by_xpath("//*[contains(text(), 'MALE')]").click()
Copy and pasted the HTML:
Please Select OptionFEMALEMALE
Inspect element of the HTML:
<ng-select _ngcontent-uau-c14="" bindlabel="cdDescr" bindvalue="cd" class="custom ng-select ng-select-single ng-select-searchable ng-untouched ng-pristine ng-valid ng-select-opened ng-select-bottom" placeholder="Please Select Option" role="listbox" title="Please Select Option" id="sex_0">
<div class="ng-select-container">
<div class="ng-value-container"><div class="ng-placeholder">Please Select Option</div><!----><!---->
<div class="ng-input" style="top: 0px;"><input role="combobox" type="text" autocomplete="ac08684b8af8" autocorrect="off" autocapitalize="off" aria-expanded="true" style="position: relative; top: 0px; left: -10px; padding-left: 10px; border: none !important; height: 45px; width: 468px;" aria-owns="ac08684b8af8" aria-activedescendant="ac08684b8af8-0"></div></div><!----><!---->
<span class="ng-arrow-wrapper"><span class="ng-arrow"></span></span></div><!---->
<ng-dropdown-panel class="ng-dropdown-panel ng-select-bottom" id="ac08684b8af8" style="opacity: 1;"><!---->
<div class="ng-dropdown-panel-items scroll-host"><div></div>
<div><!----><!---->
<div class="ng-option ng-option-marked" role="option" aria-selected="false" id="ac08684b8af8-0"><!----><!----><span class="ng-option-label">FEMALE</span></div>
<div class="ng-option" role="option" aria-selected="false" id="ac08684b8af8-1"><!----><!----><span class="ng-option-label">MALE</span></div>
<!----><!----><!----><!----></div></div><!----></ng-dropdown-panel></ng-select>
The problem:
It will select FEMALE instead of MALE because the XPath text "FEMALE" contains the text "MALE". I assume that the 'id="ac08684b8af8"' is randomly generated with each use, which means I can't use Selenium find element by id. I tried select using selenium but I get the error: "element of type ng select not select". Hence I used XPath. The "ng-option-marked" is just a feature when the cursor hovers above the dropdown selection.
How should I solve this problem?
Answer is in the comments by KunduK.
driver.find_element_by_xpath("//*[text()='MALE']").click()

Display text in Python Selenium

I'm trying to display text in what seems to be a container in web using selenium webdriver for python.
Here is the inspect element -
<div style="overflow-x: hidden;">
<div class="view-container" style="flex-direction: row;
transition: all 0s ease 0s; transform: translate(0%, 0px); direction: ltr;
display: flex; will-change: transform;">
<div aria-hidden="false" data-swipeable="true" style="width: 100%; flex-
shrink: 0; overflow: auto;">
<div>
<div class="qs-text">What is the answer to this question?</div>
I want it to display "What is the answer to this question"
I'm trying to use the below, but it doesn't return anything. -
element = driver.find_element_by_class_name("qs-text").text
print(element)
I tried find_element_by_css_selector("qs-text").text but that didn't help either.
Can you please tell me what I'm doing wrong
How about this:
element = driver.find_element_by_xpath("//*[contains(#class, 'qs-text') and contains(text(), 'answer')]")
print(element)

Selecting a value from drop down with span id in Selenium PYTHON

I have been trying this for a while and searched different forums but I couldn't find any snippet to do this.
I have a report in which options need to be selected through dropdown using Selenium in python. Below is the HTML structure
<div align="center"> Select Fruit</div>
<p align="center"><br>
<span id="0e6b87875e914a5f8d72bbee6844bea3" style="color: black; font-family: Arial; font-size: 13px; font-weight: normal; font-style: normal; width: 113px; display: inline-block;" class="sf-element sf-element-control sfc-property sfc-dropdown">
<div class="sf-element sf-element-dropdown" title="" style="position: relative; width: 100px;">
<div class="sf-element sf-element-icon" style="position: absolute; top: 1px; left: 91px; height: 17px; width: 17px;">
<svg width="17px" height="17px"><path d="M4,6 l7,0 l-3.5,3.5 z" fill="currentColor" stroke-width="1" transform="scale(1.1333333333333333,1.1333333333333333)" class="Down"></path></svg>
</div>
<div class="sf-element sf-element-text-box" style="display: inline-block; word-wrap: break-word; width: 83px;">(None)</div>
<select class="sf-dropdown-select" style="color: rgb(0, 0, 0); font-family: Arial; background-color: rgb(248, 248, 248);">
<option value="0" selected="selected">(None)</option>
<option value="1">Apple</option>
<option value="2">Mango</option>
<option value="3">Grapes</option>
</select>
</div>
</span><br></p>
I have tried different ways using css selector and XPath but nothing seems to work. Below is the code I tried
driver.find_element_by_xpath('//*[#id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[#value = "Mango"]')
Also different variants like options[2] and using css selector but it always give NoSuchElementException.
Can someone please share some insights on this?
Thanks
Add text()="Mongo" instead of #value="Mongo"
driver.find_element_by_xpath('//[#id="0e6b87875e914a5f8d72bbee6844bea3"]/div/select/option[text() = "Mango"]').click()
The dropdown is clearly within <Select> tag. Hence it would be convinient to use the Select Class as follows:
//import
from selenium.webdriver.support.ui import Select
//code block
selectOption = Select(driver.find_element_by_class_name("sf-dropdown-select"))
selectOption.select_by_visible_text("Mango")
When dealing with SELECT elements, there is a helper class, Select, that makes it a lot easier.
select = Select(driver.find_element_by_css_selector("#0e6b87875e914a5f8d72bbee6844bea3 select"))
select.select_by_visible_text("Mango")

Unable to select value in a list from selenium python script

I have been trying to automate few action on a web based tool using selenium with python as scripting language.
So in this page, I have a field called status, here is the HTML code for this.
<div style="padding-left:105px" id="x-form-el-BugFieldsEditor_Status-desc" class="x-form-element x-form-el-BugFieldsEditor_Status-desc" role="presentation">
<div class=" x-form-field-wrap x-component " role="combobox" id="BugFieldsEditor_Status-desc" style="width: 230px;">
<input type="text" class=" x-form-field x-form-text x-triggerfield-noedit" id="BugFieldsEditor_Status-desc-input" name="Status-desc" tabindex="1" readonly="" autocomplete="off" aria-owns="x-auto-462" aria-selected="" style="width: 205px;" aria-readonly="false" aria-invalid="false" aria-required="false">
<img class="x-form-trigger x-form-trigger-arrow" src="https://someurl/clear.gif" id="x-auto-463">
</div>
</div>
it actually appears as a compo box drop down list on the page. I am able to locate this element through find element by ID and able to simulate click event which brings up the list of option it contains. the HTML code of this is this.
<div role="presentation" id="x-auto-881" class="x-combo-list x-ignore x-component x-border " style="border-width: 1px; z-index: 1060; visibility: visible; height: 273px; width: 228px; left: 277px; top: 75px;">
<div tabindex="0" hidefocus="true" id="x-auto-462" class=" x-view x-combo-list-inner x-component x-unselectable " style="overflow-x: hidden; padding: 0px; border-width: 0px; height: 273px; width: 228px;" unselectable="on">
<div class="x-combo-list-item " role="listitem">A-Assigned</div>
<div class="x-combo-list-item " role="listitem">C-Closed</div>
<div class="x-combo-list-item " role="listitem">D-Duplicate</div>
<div class="x-combo-list-item " role="listitem">F-Forwarded</div>
<div class="x-combo-list-item " role="listitem">H-Held</div>
<div class="x-combo-list-item " role="listitem">I-Info_req</div>
<div class="x-combo-list-item " role="listitem">J-Junked</div>
<div class="x-combo-list-item " role="listitem">M-More</div>
<div class="x-combo-list-item " role="listitem">O-Opened</div>
<div class="x-combo-list-item x-view-highlightrow x-combo-selected" role="listitem">P-Postponed</div>
<div class="x-combo-list-item " role="listitem">R-Resolved</div>
<div class="x-combo-list-item " role="listitem">U-Unreproducible</div>
<div class="x-combo-list-item" role="listitem">W-Wait</div>
</div>
</div>
Now the issue, on simulating click, though the list of option is displayed but I am not able to locate this element and select any option.
Can someone please help.
You should be able to get it with an xpath selector like this:
driver.find_element(By.XPATH, '//div[text()="A-Assigned"]')
You may need to make the selector more specific depending on the rest of your HTML.

Categories