Print Text which is in a text Box PYTHON SELENIUM - python

im trying to print out text which is in a textbox, the HTML for the text box is:
<input type="text" class="fillIn" disabled="disabled" spellcheck="false" style="width: 36px;">
I am trying to print it on the terminal like this
but i am unable to, I have tried input.get_attribute('value')
but that doesn't seem to work either. Thanks in advance.
EDIT: i forgot to say that its a box on a webpage, im using selenium.

See if this works:-
inpValue = "-4"
inputElm = driver.find_element_by_xpath(".//input[#class='fillIn']")
driver.execute_script("arguments[0].removeAttribute('disabled')", inputElm)
driver.execute_script("arguments[0].value = '"+inpValue + "'", inputElm)
print(inputElm.get_attribute("value"))

So just consider we entered Example in input box
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://www.google.com/")
#identify element
l= driver.find_element_by_name("q")
l.send_keys("q")
#get_attribute() to get value of input box
print("Value of input box: " + l.get_attribute('value'))
driver.close()
I think it can solve your problem since you use selenium and it will return to you Example
You can check entire topic from this link

Related

how to change select value use python selenium

I want to get selected option using Selenium WebDriver with Python
but I used select_by_value can't get vaule
I have the following HTML code
<select id="play_date" name="play_date" onclick="javascript:GoodsInfo.GetPlayDate(this);" onmouseover="javascript:GoodsInfo.GetPlayDate(this);" onchange="javascript:GoodsInfo.GetPlayTime(this.value);">
<option value="" selected="">Select Date</option>
<option value="20230202" style="color: black;">Thu, Feb 02, 2023</option></select>
I am trying to get a list of the option values ('20230202') using Selenium.
At the moment I have the following Python code
select_date = Select(wait.until(EC.element_to_be_clickable((By.XPATH, "//*[#id='play_date']"))))
select_date.select_by_value('20230202')
but it's always have is
selenium.common.exceptions.NoSuchElementException: Message: Could not locate element with index 1
Any help or pointers is appreciated, thank you!
If you get error NoSuchElementException it means that you are using a wrong xpath/css_selector or that the element is inside an iframe. You can check by looking at the HTML
So your case is the latter, and to make the xpath work you have first to switch to the iframe, using driver.switch_to.frame() or better EC.frame_to_be_available_and_switch_to_it.
WebDriverWait(driver, 9).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "product_detail_area")))
dropdown_date = WebDriverWait(driver,9).until(EC.element_to_be_clickable((By.ID, "play_date")))
options = []
# open menu so that options are loaded
dropdown_date.click()
# wait until options are loaded
while not options:
options = driver.find_elements(By.CSS_SELECTOR, "#play_date option:not([value=''])")
time.sleep(1)
# close menu
dropdown_date.click()
print('available options:')
for opt in options:
print(opt.get_attribute('value'))
select_date = Select(dropdown_date)
select_date.select_by_value(input('enter value: '))

How to copy the text of an input, which is not in the html code with selenium Python

I have a problem with a input, because its value do not store in html code.
This is the html code
<input spellcheck="false" class="editablesection" onkeypress="checkKey(event)" oninput="writinginput(this, 0)" maxlength="11" style="width: 113px;" readonly="">
Image of input with text
I search the element with this:
input = driver.find_element_by_css_selector(".editablesection")
But i cant copy the text because its not store in html code.
How i can do it?
Site where input its located:
https://www.w3schools.com/html/exercise.asp?filename=exercise_html_tables6
You need to click "Show Answer" button to see the text i want to copy.
You can use selenium get_attribute("value") method to extract the value.
Below is the code from selenium python:
driver = webdriver.Chrome(PATH)
driver.get('https://www.w3schools.com/html/exercise.asp?filename=exercise_html_tables6')
driver.maximize_window()
sleep(3)
driver.find_element(By.XPATH,"//button[contains(.,'Show Answer')]").click()
sleep(3)
readOnlyField = driver.find_element(By.CSS_SELECTOR,"#showcorrectanswercontainer > .editablesection")
print(readOnlyField.get_attribute("value"))
driver.quit()
Output:
rowspan="2"

Log in to instagram using selenium

I am a novice coder. Trying my hand at creating an instagram bot and I'm following the instructions found here: (https://realpython.com/instagram-bot-python-instapy/)
I can fill in the elements for user name and password, but I'm having trouble clicking the login link. Using the -
#submit = browser.find_element_by_tag_name('form')
#submit.submit()
portion of my code works to log in, but I would like to be able to use find element by xpath and for this so I can apply it in different situations. If someone could please let me know where I can look in the HTML on the instagram page to find what I'm looking for, or direct me towards any helpful reading, I'd really appreciate it! I've tried a few ways to do this.
Here is my code. I also added photos of my code and error message.
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#sets browser to firefox - need to make sure that geckodriver and firefox are in the path
browser = webdriver.Firefox()
#opens firefox to instagram#
browser.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
#tells selenium to wait 5 seconds before trying again if it can't find something
browser.implicitly_wait(10)
print ('i waited')
user = 'username'
password = 'password'
##### USERNAME #####
username = browser.find_element_by_xpath("//input[#name='username']")
username.send_keys(user)
#username = browser.find_element_by_name('username')
#username.send_keys(user)
##### PASSWORD #####
password = browser.find_element_by_xpath("//input[#name='password']")
password.send_keys(password)
#password = browser.find_element_by_name('password')
#password.send_keys(password)
browser.implicitly_wait(10)
##### LOG IN #####
#instead of searching for the Button (Log In) you can simply press enter when you already selected the password or the username input element.
#submit = browser.find_element_by_tag_name('form')
#submit.submit()
#login_link = browser.find_element_by_xpath("//article/div/div/p/a[text()='Log in']")
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
login_link.click()
#login_form = browser.find_element_by_xpath("//form[#id='loginForm']")
#form.submit()
print ('login incomplete :)')
sleep(5)
browser.quit() #quits geckodriver and mozilla
print ('closed')
Picture of error
code part 1
code part 2
Your xpath is bit wrong. you are using
//a[text()='Log in']
but Log in is inside div tag, not a tag.
Please use this xpath
//div[text()='Log In']/..
In your code
login_link = browser.find_element_by_xpath("//div[text()='Log In']/..")
login_link.click()
The HTML is
<div class=" qF0y9 Igw0E IwRSH eGOV_ _4EzTm ">Log In</div>
and we are using text() to target text Log in and then we are looking for button using /.. which is a parent node.
Replace this link
login_link = browser.find_element_by_xpath("//a[text()='Log in']")
with
login_link = browser.find_element_by_xpath("//div[contains(text(),'Log In')]")
And you should be good. Since the Login In button is wrapped inside a div element and not a element.
As already explained Log In is in a div tag under a button tag with type submit. You can use this xpath too.
browser.find_element_by_xpath("//button[#type='submit']").click()

Python selenium ElementNotInteractableException: Message: element not interactable

I am trying to login to beeradvocate.com to scrape (crawl) some beer data.
I tried with selenium but have been brutally failing.
here is the html
<input type="text" name="login" value="" id="ctrl_pageLogin_login" class="textCtrl" tabindex="1" autofocus="autofocus" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR4nGP6zwAAAgcBApocMXEAAAAASUVORK5CYII="); cursor: auto;">
i tried using name and value and class but everything has failed.
I attempted Xpath as my final try, but have failed as well.
website and inspection
My code:
driver=webdriver.Chrome("~~~~\\chromedriver.exe")
driver.get("https://www.beeradvocate.com/community/login/")
from selenium.common.exceptions import TimeoutException
driver.maximize_window()
while True:
try:
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="ctrl_pageLogin_login"]'))).send_keys("scentmaster")
break
except TimeoutException:
print("too much time")
I've made the button work with:
button = driver.find_element_by_xpath('//*[#id="pageLogin"]/dl[3]/dd/input')
driver.execute_script("arguments[0].click();", button)
However, I need to be able to perform sent_keys to type in id and pw to log in...
Anybody got some idea?
There are 2 fields on the page if you use xpath //*[#id = "ctrl_pageLogin_login"], the input field you are referring to is the second. Sadly, the selenium find element by default refers to the first. It will work if you make it like this: (//*[#id = "ctrl_pageLogin_login"])[2].
But I have another suggestion, try to locate element by css selector with this value : form#pageLogin input#ctrl_pageLogin_login
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'form#pageLogin input#ctrl_pageLogin_login'))).send_keys("scentmaster")
#for password
driver.find_element_by_css_selector('form#pageLogin input#ctrl_pageLogin_password').send_keys('your_password')
#for submit
driver.find_element_by_css_selector('form#pageLogin input[type=submit]').click()
To feed the username, try this xpath:
'//form/dl/dd/input[#id="ctrl_pageLogin_login"]'
for the password:
'//form/dl/dd/input[#id="ctrl_pageLogin_password"]'
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'form#pageLogin input#ctrl_pageLogin_login'))).send_keys("scentmaster")
#for password
driver.find_element_by_css_selector('form#pageLogin input#ctrl_pageLogin_password').send_keys('your_password')
#for submit
driver.find_element_by_css_selector('form#pageLogin input[type=submit]').click()
The solution above given by frianH worked! :)

Python3 (Selenium) can't locate password box

my current problem is that Python can't locate my password box.
<input class="stylepwd" name="pws" size="12" maxlength="12" onkeypress="return stEnter(event,this);" autocomplete="off" type="password">
Thats all i tried to do, but it always say it cant locate that box
element = driver.find_element_by_name('pws')
element = driver.find_element_by_class_tag('stylepwd')
element = driver.find_element_by_id('') #Yea, thats obviously not working ^^'
element.send_keys('mypassword')
Maybe there's another way I can type in that password. As soon as the site is loading, the cursor is in the box. Can I do anything with this? (Thats the site: http://imgur.com/a/6OIgm)
Python code: https://pastebin.com/3ydHwDkH
You can do something like this :
elem = driver.find_element_by_xpath('//input[#class="stylepwd"]')
This will give you the element input with class="stylepwd"
If it is in iframe, try
iframe = driver.find_elements_by_tag_name("iframe")[0]
driver.switch_to_frame(iframe)
then
element = driver.find_element_by_name('pws')

Categories

Resources