I am trying to locate and click an element by text. The reason is that the id and location of the element are dynamic but the text is always the same.
Here it is how look like as a HTML:
<div class="p-r" input-title="I want to identify by text here">
<!---->
<div class="skDeleteBtn skRequired">
<input class="input radio--big ng-untouched ng-pristine ng-valid" data-cname="radibutton" type="radio" id="z1jcfqy9">
<label class="label label--radio-big" for="z1jcfqy9" style="color: inherit;">
<span></span>I want to identify by text here
<i class="d-ib va-m ml-3">
<sk-tooltip><!---->
</sk-tooltip>
</i>
</label>
</div>
</div>
I want to identify by text I want to identify by text here.
I want to mention that the location of the text where starts with <span></span> is a little bit weird. Specifically, i think that the text has whitespace and endline after the text, so I prefer to locate and click with input-title="I want to identify by text here">.
So far I tried:
driver.find_element_by_xpath("//*[contains(text(),'{}')]".format('I want to identify by text here'))
driver.find_element_by_xpath("//*[contains(text(),'I want to identify by text here')]")
but no luck.
Thanks in advance!
You just want to identify and click the input-title element? This should work, by text
driver.find_element_by_css_selector('div[input-title="I want to identify by text here"]').click()
Related
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)
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
F1, need some help or hints with Hidden element using Robotframework.
The problem consist that to fill any text in the text area, I need to change the state of text area from display:none; to display:block;
Needed text area for input
Code that I see from WebDev Tool
The code itself:
<div class="col-md-12">
<div class="cazary" style="width: 664px;">
<div class="cazary-commands-wrapper">
<ul class="cazary-commands-list">
<li unselectable="on" title="Size" class="cazary-command-fontsize">Size</li>
</ul>
<ul class="cazary-commands-list">
<li unselectable="on" title="Foreground Color" class="cazary-command-forecolor">Foreground Color</li>
</ul>
<ul class="cazary-commands-list">
<li unselectable="on" title="Background Color" class="cazary-command-backcolor">Background Color</li>
</ul>
<ul class="cazary-commands-list">
<li unselectable="on" title="Remove Format" class="cazary-command-removeformat">Remove Format</li>
</ul>
</div>
<iframe class="cazary-edit" src="javascript:" style="height: 39px;"></iframe>
<textarea id="summernote" class="required-preview field cazary-source" placeholder="Tell us all about your Advertisement. This description will be prominently displayed in your Advertisement notice. Feel free to adjust the fonts and background colour too." name="observations" cols="50" rows="10" style="display: none;"></textarea>
</div>
My Robotframework code tries:
Select Frame //iframe[#class="cazary-edit"]
# First try
Input text //textarea[#id="summernote"] ${UniversalVariableforName}
# Second try
Input text //iframe[#class="cazary-edit"] ${UniversalVariableforName}
# Third try
Input text //div[#class="cazary"]//iframe[#class="cazary-edit"] ${UniversalVariableforName}
# Fourth try
Input text //body[#class="empty"] ${UniversalVariableforName}
# Fifth try
Input text //iframe[#class="cazary-edit"]//body[#class="empty"] ${UniversalVariableforName}
Errors that were returned:
image
May be there is a solution with Execute Javascript keyword?
The concerned <textarea> is outside of the <iframe class="cazary-edit">. Hence we don't need to switch to the <iframe>
To send the text to the Input field you can try to :
Use xpath as :
"//textarea[#class='required-preview field cazary-source' and #id='summernote']"
Click the Input field first.
Next Clear the Input field.
Finally try to send the text.
Update :
As the concerned textarea have the style attribute set as "display: none;", we have to change to "display: block;" through JavascriptExecutor then send the text.
Python Sample Code :
driver.execute_script("document.getElementById('ID').style.display='block';")
Below is the element in DOM.
<div style="float:left;width: 40px">
<label class="radio inline">
<input id="formData99991:Select" class="sample" data-ng-model="sampletId" size="" name="formData99991Select" sample-value="true" style="" value="1234" checked="checked" type="radio"/>
A
</label>
</div>
I tried with //input[contains(#id,'formData99991:Select') and contains(text(),'A')] but it doesn't work.
There are multiple similar elements on the web page and I need to identify each element with combo of id and text.
You can try with following x-path,
//label[contains(.,'A')]/input[#id="formData99991:Select"]
I'm trying to send some text to the search textarea at Google Adwords Keyplanner tool using Selenium python.
When inputting manually the text from textarea goes to its attribute 'title'.
The textarea looks like this:
<textarea class="spl-a sprb-c spl-d" rows="1" id="gwt-debug-keywords-text-area" aria-labelledby="gwt-uid-94 gwt-uid-74" wrap="off" style="overflow: hidden;" dir="ltr" title="mytext"></textarea>
As a result:
driver.find_element_by_tag_name('textarea').send_keys('mytext')
or
driver.find_element_by_id('gwt-debug-keywords-text-area').send_keys('mytext')
produces
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
Is there another way to send keys to this search textarea? I have like 500 keywords which I need to grab data for.
full code of text area:
<div class="spl-c spI-d" style="width: 575px;">
<div id="gwt-uid-119" class="sprb-a">
<div class="sprb-b spl-b" style="display: none;" aria-hidden="true">For example, flowers or used cars</div>
<textarea id="gwt-debug-keywords-text-area" class="spl-a sprb-c spl-d" rows="1" aria-labelledby="gwt-uid-139 gwt-uid-119" style="overflow: hidden;" dir="ltr" title="my keyword" wrap="off"></textarea>
</div>
</div>
</div>
The element you are trying to interact with is but addressable by Selenium, because it is inside a div which is hidden - this one:
<div class="sprb-b spl-b" style="display: none;" ...
There is another element which is actually taking the user's input - look for elements with keypress event handlers, and also what changes in DOM web you enter characters.