Python input text to randomly changed element without id or name - python

How can I input a text to a span element which is changing randomly at every page load?
I want to write a text on facebook with python but fb is changing span class etc to random at every page load...
If you load https://www.facebook.com/messages page the message input "Aa" is empty... and the element is the message is: <br data-text="true">
but if you write something it changes to: <span data-text="true">test message</span>
I tried:
message_input = driver.find_element_by_css_selector("br[data-text='true']")
message_input.send_keys('test message')
but ofc it makes no sense... not working.
Is there any way I can put text in that span element without importing anything?

Related

Python Selenium lists only one li repeatedly

I'm trying to make an attendance app, using Selenium; the script should read the names and click on a designated checkbox when a name is matched. What happens is that it keeps clicking on the first checkbox repeatedly. When I looked into it turns out that it only reads the first li only.
here's the HTML code
<div class="members">
<ul class="memberlist"></ul>
</div>
the li are generated via fetch API from the database
when I try these in python, it only reads the first li element over and over
for i in range(len(df)):
print(driver.find_element(By.XPATH,".//ul[#class='memberlist']/li/p").text)
for i in range(len(df)):
print(driver.find_element(By.CLASS_NAME,"name").text)
also, I tried the solution from this and I don't think this is the correct syntax
selenium python for loop printing only first element repeatedly
Update: as per dosas comment, this did list all the names instead of just one, then I wrote this to click on the checkboxes of the attendees.
for i in range(len(df)):
# match names of the sheet with the 'name' class in the webpage
if df['name'][i]==driver.find_elements(By.CLASS_NAME,"name")[i].text:
driver.find_elements(By.CLASS_NAME, week)[i].click()
print(driver.find_elements(By.CLASS_NAME,"name")[i].text)
else:
print('No match')
Now I have a different problem, the attendance excel sheet has to be in the same order as the list on the web page, otherwise, it'll show 'No match'. So how do I make it search the whole page for each particular name?

Selenium Python, send number input value to li in HTML

I am automating a script that selects a product quantity, but this particular site uses +/- click buttons and has no dropdown or input field for the quantities that are read from CSV.
Rather than emulating button presses of the +/- for the quantities, you can manually edit the HTML below with the desired number and click the add to cart for the same effect.
Is it possible to edit the HTML when there is no form to do so on the site?
Or do I have to emulate clicks for the desired quantity.
I am using Pandas to read the CSV and is working code from another project.
Thank you if you can help
https://www.matsukiyo.co.jp/store/online/p/4902888248269
<ul class="count">
<li>数量</li>
<li class="minus disable">-</li>
<li class="num" id="addToCartQty" data-code="99">1</li>
<li class="plus">+</li>
</ul>
You can change the data-code="99">1</li> from 1 to 99 and it will update but is this possible in Selenium as I cannot find the right selector to do so. I have a feeling this is not possible?
The current code I am trying is sending clicks but I am not able to send the number of clicks I want and am stuck for a solution either way.
How can I send the value from the CSV to dictate the number of clicks?
Or how can I write the value to the li HTML?
driver.get(web_url+str(UPC[i]))
try:
driver.find_element(By.LINK_TEXT, "+").click
# I want to add the number of clicks here(str(quantity[i]))
link = driver.find_element_by_css_selector("p.button.cart")
link.click()
except:
print("Product Number: "+str(UPC[i])+" Sold Out!")
As I can see the element containing the count is located by id='addToCartQty' so what you can do is simply
driver.find_element_by_css_selector('#addToCartQty').sendKeys("your value");
or
driver.find_element_by_css_selector('#addToCartQty').sendKeys("value", "your value");
or
element = driver.find_element_by_css_selector('#addToCartQty')
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].value='enter the value here';", element);
Don't forger to change "your value" or 'enter the value here' with a number you wish to input there.
It is possible to edit the html and execute the script using execute_script() method
script="document.getElementsByClassName('num')[0].value="+your_value+" ;"
driver.execute_script(script)
You can check this by running the script in your browser console,it will induce your required value to the html tag.

Wait for the element to be selected in Selenium

There is a text field which gets automatically populated after a certain time by the website and the the text cursor moves to the same text field
<html>
<div>
<input type=text id=name>Enter here</div>
<html>
the input field gets selected and filled automatically by the website after a few seconds. I want selenium to wait till it gets selected and then carry on with the further code.
I've done:
wait=WebDriverWait(driver,10)
element=driver.find_element_by_id("name")
wait.until(expected_conditions.element_to_be_selected(element))
It throws error even though the text field is filled by the website.
The expected_conditions.element_to_be_selected() follows this spec. Specifically:
This operation only makes sense on input elements of the Checkbox- and Radio Button states (emphasis mine)
Your input is of type text, so it will not work.
You will have to use a different approach. Perhaps something like:
Read the text (the #value attribute).
Wait until it changes to anything else.
There were a few answers on other similiar questions which seem to work for respective questions. I did not find anything for python though.
What i did is run an infinite loop until the text present in the element had length not equal to zero.
while 1 is not 5:
if len(element.get_attribute("value")) is not 0:
break
else:
continue
value attribute of element has the value of whatever string is present in the textfield.

How to get the text of an element if that is only present in devtool using robot framework?

I want to get the text "5831" from this element which is only present in devtool, I already tried using this following code but the can't still get text.
${td_inputverificationpin} get text ${textPin}
page should contain ${td_inputverificationpin}
log to console verifcation code: ${td_inputverificationpin}
input text ${inputPin} ${td_inputverificationpin}
This is my code
(https://i.stack.imgur.com/5nWKQ.jpg)
My locator(xpath)
(https://i.stack.imgur.com/p5HEQ.jpg)
The result. It is supposed to be
verification code: 5831
(https://i.stack.imgur.com/GdWOD.jpg)

With Selenium/Python how do I get the page viewable text of a checked radio button (or checkbox)?

I have a form that I am processing where I need to get the text values of all checked radio buttons (one radio button checked per line item of course).
The HTML is like so (for a radio button that is checked):
<div style="white-space:nowrap;margin-right:15px;display:inline-block;">
<input
type="radio"
name="question_id[927]"
id="question_id[927]"
value="276"
class="noborder"
checked="">Blue
</div>
Even though the checked="" attribute value is empty, selenium/python will return correctly that it is checked by calling: is_checked = element.get_attribute('checked'), so that works.
The other radio buttons in the line item, that are not checked, simply leave out the checked="" attribute.
I need to get the word Blue into a variable.
After experimenting with many things, I cannot seem to extract the text beside the radio button.
I am successfully finding the list of radio button 'Element's', iterating through those and successfully finding which one is checked, and now only need to get the text value of the radio button that is checked to store that value in a database.
webdriver.get_attribute() only works with items inside the <input..> tag.
The text I need to get is enclosed by a <div>, and all radio buttons in the list are enclosed by the same <div> text.
I need to do the same with a list of checkbox values, getting the text from each one that is checked, and the construction is the same, with the text just after the <input...> tag and just before the closing </div>
I have searched every selenium / python article I could find, and am given no clue as to how to get this text.
It would be greatly appreciated if anyone anywhere has a working code line/snippet that would work to get this.
thank you,
It seem like you want to get the div tag text from the parent of the current input tag, so try using the parent approach like the following:
elems = driver.find_elements_by_xpath('//input[#checked]//parent::div')
print(len(elems))
for elem in elems:
print(elem.text)
Reference: XPath Axes

Categories

Resources