Selenium (Python) - getting the value inside a button - python

So I'm trying to get the value inside a button using selenium.
My code is:
getphone = profile.find_element_by_class_name('app-emp-phone-txt').click().find_element_by_tag_name("p")
But I'm getting this error:
AttributeError: 'NoneType' object has no attribute 'find_element_by_tag_name'
What should I write in my code ?
thanks a lot :)

You're trying to call find_element from method (click) call which returns None. Try to update your code:
getphone = profile.find_element_by_class_name('app-emp-phone-txt')
getphone.click()
getphone.find_element_by_tag_name("p")
Update
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://www.mariages.net/domaine-mariage/la-tour-des-plantes--e129089')
getphone = driver.find_element_by_class_name('app-emp-phone-txt')
getphone.click()
number = driver.find_element_by_css_selector(".app-dropdown-show-phone>.storefrontDrop__tag").text
print(number)

Related

How To Fix TypeError: 'FirefoxWebElement' object is not subscriptable In Python Selenium

I'm Creating Whatsapp Script. I Have 8k Contacts I Want To Save Them All In Txt File. I'm ​Creating This Script in Selenium. But I'm Getting Error TypeError: 'FirefoxWebElement' object is not subscriptable
Here Is My Code:
from selenium import webdriver
from selenium.webdriver.common import keys
import time
driver = webdriver.Firefox(executable_path="geckodriver.exe")
driver.get("https://web.whatsapp.com/")
input("Qr Code Done? : ")
driver.implicitly_wait(10)
phnenumbers = driver.find_elements_by_class_name('zoWT4')
for number in phnenumbers:
print(number[0].text)
Thanks For Your Important Time And Help!
driver.find_elements_by_class_name() returns a list of elements, so you probably want
for number in phnenumbers:
print(number.text)

AttributeError: 'str' object has no attribute 'send_keys' error using Selenium in Python

This is the code from my Twitter Bot Project.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class TwitterBot:
def __init__(self,username, password, search_text):
self.driver = webdriver.Chrome()
self.driver.get("https://twitter.com/home?lang=en")
time.sleep(2)
# Enter your username
self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input')\
.send_keys(username)
# Enter your password
self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input') \
.send_keys(password)
self.driver.find_element_by_xpath('/html/body/div/div/div/div[2]/main/div/div/div[1]/form/div/div[3]/div/div')\
.click()
time.sleep(3)
# Enter text in the search box
self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')\
.send_keys(search_text)
search_text.send_keys(Keys.ENTER)
time.sleep(4)
while True:
pass
TwitterBot("rmail#gmail.com", "abcd1234", "lamborghini")
When I try to run this script, I am getting an AttributeError.
File "C:\Users\Praneeth Ravuri\PycharmProjects\Twitter Bots\Open Twitter Bots .py", line 24, in __init__
search_text.send_keys(Keys.ENTER)
AttributeError: 'str' object has no attribute 'send_keys'
Can someone solve my problem and edit this code?
The .send_keys(...) method belongs to the WebElement, not the string.
It's what causes your code to produce this error:
AttributeError: 'str' object has no attribute 'send_keys'
Instead of this line:
# Enter text in the search box
self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')\
.send_keys(search_text)
search_text.send_keys(Keys.ENTER)
You can change with this code:
search_box = self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')
search_box.send_keys(search_text)
search_box.send_keys(Keys.ENTER)
You should initialize search_box as WebElement, put the text then submit with enter keys.
This error message...
AttributeError: 'str' object has no attribute 'send_keys'
...implies that you script/program have attempted to invoke send_keys() on a string object.
What went wrong
As per the line of code:
search_text.send_keys(Keys.ENTER)
You are trying to invoke send_keys() on the variable search_text which is of type string passed to def __init__(self,username, password, search_text) method. Where as send_keys() is a method associated with WebElement. Hence you see the error.
Solution
You need to invoke send_keys() on the WebElement as follows:
self.element = self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')
self.element.send_keys(search_text)
self.element.send_keys(Keys.ENTER)
Refrences
You can find a relevant detailed discussion in:
AttributeError: 'str' object has no attribute 'click' while trying to loop through the hrefs and click them through Selenium and Python
I don't use twitter so I don't exactly know what search box you are talking about, but If you just want to enter some text in the search box and hit Enter, Then, replace this:
# Enter text in the search box
self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input').send_keys(search_text)
search_text.send_keys(Keys.ENTER)
with this:
# Enter text in the search box
element = self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div[2]/div/div/div/div[1]/div/div/div/form/div[1]/div/div/div[2]/input')
element.send_keys(search_text)
element.send_keys(Keys.ENTER)
I can't test this on my machine as I don't use twitter but I think it should work. Please let me know if this helps. Thanks

how do I print the contents of a <cite>?

Im trying to pull the links with python that google provides and idk why it doesnt work.
the error :
AttributeError: 'WebElement' object has no attribute 'text'
my code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
input = input('what do you want to *scrape* ?\n>')
driver = webdriver.Opera()
driver.get('https://google.com')
print(driver.title)
search = driver.find_element_by_xpath('//*[#id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input')
search.send_keys(input)
search.send_keys(Keys.RETURN )
headers = driver.find_elements_by_tag_name('h3')
cites = driver.find_elements_by_tag_name('cite')
for cite in cites :
print(cite.text)
time.sleep(5)
print('done')
driver.quit()
I just ran your code and it worked fine after providing the path of the Operadriver executable
driver = webdriver.Opera(executable_path=r"C:\operadriver.exe)
Try to cite.getAtrribuet("innerHTML")
Or cite.getAttribute ("outterHTML")
Or excuter.executeScript("arguments[0].innerHTML",cite) or replace inner to outter

Python Selenium "WebDriverElement' object has no attribute 'get_attribute'

I am using Selenium and Splinter to run my UI tests for my web application. I am generating a random id for the views on the page and would like to select the random ID for testing.
Here is the code I am using
from selenium import webdriver
from splinter import Browser
executable_path = {'executable_path':'./chromedriver.exe'}
browser = Browser('chrome', **executable_path)
data_view_id = browser.find_by_xpath('//ul[#class="nav"]').find_by_xpath('.//a[#href="#"]')[0].get_attribute("data-view-id")
# I am trying to get the id for the first item in the nav list and use it elsewhere
print(data_view_id)
This is the error I am receiving:
AttributeError: 'WebDriverElement' object has no attribute 'get_attribute'
I've looked at the 'readthedocs' page for WebElement and it has the 'get_attribute' value. I cannot find any documentation regarding WebDriverElements and need help accessing the WebElement instead
That WebDriverElement is from Splinter, not Selenium.
In Splinter, you access attributes like a dict (see the Splinter docs)
data_view_id = browser.find_by_xpath('//ul[#class="nav"]').find_by_xpath('.//a[#href="#"]')[0]['data-view-id']
Or if you wanted to do it in Selenium:
browser.find_element_by_xpath('//ul[#class="nav"]').find_element_by_xpath('.//a[#href="#"]').get_attribute("data-view-id")

Error in Typing on Input Field Using Python Selenium

My goal is to be able to open and navigate a web page based on the inputs I enter in Python.
Currently I am having issues inserting a string input into "Street #" field, and keep getting the below error:
AttributeError: 'list' object has no attribute 'send_key'
Here is my code:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary(r"C:...\firefox.exe")
# Open a Web Browser
browser = webdriver.Firefox(firefox_binary = binary)
# Open the 1st Page
browser.get("http://sftreasurer.org/property-tax-payments")
elem_1stClick = browser.find_element_by_css_selector(".ttx_button > div:nth-child(1)")
elem_1stClick.click() # Click on it
# On the 2nd Page
elem_streetNumber = browser.find_elements_by_css_selector("#ContentPlaceHolder1_tbStreetNumber")
elem_streetNumber.send_key("1230")
elem_2ndClick = browser.find_element_by_css_selector("#ContentPlaceHolder1_btnSearch")
elem_2ndClick()
Can anyone help me solve this issue? Your help would be greatly appreciated.
You should replace this line
elem_streetNumber = browser.find_elements_by_css_selector("#ContentPlaceHolder1_tbStreetNumber")
with this
elem_streetNumber = browser.find_element_by_css_selector("#ContentPlaceHolder1_tbStreetNumber")
This is because find_elements_by_css_selector() returns list of web-elements while find_element_by_css_selector() returns just a single web-element

Categories

Resources