How to use Selenium to input text in Python - python

I want to input "apple" instead of "hello" using Selenium in Python.
<span><div class="top1"><div class="top2"><label class="top3">
<label class="check"><input class="word" type="text" value="hello"></label>
How to do it?
Thank you very much.

As per the HTML you have provided, to send the text apple within the <input> element you can use the following code block :
driver.find_element_by_xpath("//label[#class='check']/input[#class='word' and #type='text' and #value='hello']").click()
driver.find_element_by_xpath("//label[#class='check']/input[#class='word' and #type='text' and #value='hello']").clear()
driver.find_element_by_xpath("//label[#class='check']/input[#class='word' and #type='text' and #value='hello']").send_keys("apple")

Before asking elementary questions like this, please read documentation for tools which you are using. You can find documentation via Google.
About your task. First, you need find your input in source code, than clean value and input a new one.
your_input = driver.find_element_by_xpath('xpath to your input')
your_input.clear()
your_input.send_keys('your new text')

Related

I'm having trouble selectiong an element using Selenium with Python

I want to read out the text in this html element using selenium with python. I just can't find a way to find or select it without using the text (i don't want that because its content changes)
<div font-size="14px" color="text" class="sc-gtsrHT jFEWVt">0.101 ONE</div>
Do you have an idea how i could select it? The conventional ways listed in the documentation seem to not work for me. To be honest i'm not very good with html what doesn't make things any easier.
Thank you in advance
Try this :
element = browser.find_element_by_class_name('sc-gtsrHT jFEWVt').text
Or use a loop if you have several elements :
elements = browser.find_elements_by_class_name('sc-gtsrHT jFEWVt')
for e in elements:
print(e.text)
print(browser.find_element_by_xpath("//*[#class='sc-gtsrHT jFEWVt']").text)
You could simply grab it by class name. It's 2 class names so it would be like so. by_class_name only uses one.
If the class name isn't dynamic otherwise you'd have to right click and copy the xpath or find a unique identiftier.
Find by XPath as long as font size and color attribute are consistent. Be like,
//div[#font-size='14px' and #color='text' and starts-with(#class,'sc-')]
I guess the class name is random?

Printing Text from 2nd Div in Class in Python + Selenium

newbie here trying to learn Selenium. I have the following HTML Code:
<div class="lower-text">
<div data-text="winScreen.yourCodeIs">Your Code Is:</div>
<div>OUTPUTCODE</div>
</div>
I am trying to only print the text OUTPUTCODE, however the following code only prints "Your Code Is:".
text = browser.find_elements_by_class_name('lower-text')
for test in text:
print(test.text)
Any help would be appreciated. Thank you.
Try the below xpath.
//div[#class='lower-text']/div[last()]
You code should be
print(driver.find_element_by_xpath("//div[#class='lower-text']/div[last()]").text)
Try below Solutions:
1. Xpath :
//div[#class='gs_copied']
2. CSS selector
.lower-text > div:nth-child(2)
Your site is unstable and not always generating coupon code.Currently I am getting below error(check screenshot). So wont able to identify elements which i have mentioned above.
You need to amend your logic based on functionality and if person is Unlucky for getting coupon code then you have to write script to handle other functionality based on your site, (e.g: Check out our Hot Deals Page)
Try the following approach:
text = driver.find_element_by_xpath("//div[text()='Your Code Is:']//following-sibling::div[text()]").get_attribute('innerHTML')
print(text)
I have copy pasted your html part in a new text file and tried the following xpath which work perfectly:
//div[#class='lower-text']/div[text()='Your Code Is:']/following-sibling::div
Attaching screenshot link also. Please have a look and hopefully it will solve your problem.
https://imgur.com/EujgZrI

Is it possible that LXML can detect what value a class has in HTML?

So for example I have something like this:
<button class="oW_lN oF4XW sqdOP yWX7d _8A5w5 ">MyTextIsHere</button>
The "MyTextIsHere" section can have only 2 different text for example: "Yes" and "No", would it be possible to somehow check out which value is at the given moment? The page reloads after every action.
Or something else that would work? I am using Python.
Using Selenium you can easily extract the text Yes or No using either of the following solutions:
Using get_attribute():
myText = driver.find_element_by_xpath("element_xpath").get_attribute("innerHTML")
Using text:
myText = driver.find_element_by_xpath("element_xpath").text

How can I find the randomly Xpath using Selenium?

I am a newer in Selenium and use python to build it. Recently, I found a question which want to ask someone who can help me to figure it. The question is the Xpath I want to get is randomly, for example:
'//*[#id="wiki-edit-wikiEdit26"]/div/div/div/div[2]/a[1]'
'//*[#id="wiki-edit-wikiEdit27"]/div/div/div/div[2]/a[1]'
'//*[#id="wiki-edit-wikiEdit28"]/div/div/div/div[2]/a[1]'
These three xpath are used on the same button, but the number after wikiEdit will be changed every time. Therefore, are there any way which can help me to run my script more smoothly? Thank you very much!
Here is my python code:
broswer.find.element_by_xpath('//*[#id="wiki-edit-wikiEdit26"]/div/div/div/div[2]/a[1]') .click()
You can use matches in xpath to do this,
broswer.find.element_by_xpath("//*[matches(#id, '^(wiki-edit-wikiEdit)[0-9]')]/div/div/div/div[2]/a[1]") .click()
so basically that matches the id anything starting wiki-edit-wikiEdit followed by numbers form [0-9]
You just need to format your string..
import random
# Random range 1 - 100
x = random.randint(1,100)
broswer.find.element_by_xpath(f'//*[#id="wiki-edit-wikiEdit{x}"]/div/div/div/div[2]/a[1]') .click()
You can use starts-with or contains
broswer.find.element_by_xpath("//*[contains(#id,'wiki-edit-wikiEdit')]/div/div/div/div[2]/a[1]").click()
Also, using such long xpath's is not recommended. Use css-selectors over xpath.

Python + Selenium: How can click on "onclick" elements?

I have an "onclick" element in a webpage whose HTML reads as follows:
Fastener
I want to search this element using the string "fastener" or "Fastener" using Python + Selenium. The number "3625" will change depending on previous inputs, and hence cannot be searched for.
I tried the following, but in vain:
br.find_element_by_css_selector("a[#onlick*='fastener']").click()
Please suggest ways to do this. Thank you!
P.S.: I am using Python 2.7, with Chrome WebDriver and Chrome v62.
To search the element with text as Fastener you can use either of the following options :
Through Fastener :
br.find_element_by_link_text("Fastener").click()
Using onclick through fastener (xpath):
br.find_element_by_xpath("//a[contains(#onclick,'fastener')]").click()
Using onclick through fastener (css_selector):
br.find_element_by_css_selector("a[onclick^='fastener']").click()
Use the following code for that:
br.find_element_by_link_text("Fastener").click()
Hope it helps you!
Using capybara-py:
page.click_link("Fastener")
Capybara is designed to provide this and many other similar helper methods, such as one might need to write acceptance tests from the perspective of end users:
page.fill_in("Street", value="123 Main St")
page.select("United States", field="Country")
page.choose("Expedited shipping")
page.click_button("Place order")
Use contains to search element
//a[contains (#onclick='fastener')]
OR
//a[contains(text(),"Fastener")]

Categories

Resources