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
Related
I am new bee on python selenium environment. I am trying to get the SQL version table from enter link description here
from selenium.webdriver.common.by import By
from selenium import webdriver
# define the website to scrape and path where the chromediver is located
website = "https://www.sqlserverversions.com"
driver = webdriver.Chrome(executable_path='/Users//Downloads/chromedriver/chromedriver.exe')
# define 'driver' variable
# open Google Chrome with chromedriver
driver.get(website)
matches = driver.find_elements(By.TAG_NAME, 'tr')
for match in matches:
b=match.find_elements(By.XPATH,"./td[1]")
print(b.text)
it says AttributeError: 'list' object has no attribute 'text'. Am i choosing the write syntax and right parameters to grab the data?
Below is the table which i am trying to get data.
enter image description here
Below are the parameters which i am trying to put in code.
enter image description here
Please advise what is required to modify in the code to obtain the data in table format.
Thanks,
Arun
If you need data only from first table:
from selenium.webdriver.common.by import By
from selenium import webdriver
website = "https://www.sqlserverversions.com"
driver = webdriver.Chrome(executable_path='/Users//Downloads/chromedriver/chromedriver.exe')
driver.get(website)
show_service_pack_versions = True
xpath_first_table_sql_rows = "(//table[#class='tbl'])[1]//tr/td/a[starts-with(text(),'SQL Server')]//ancestor::tr"
matches = driver.find_elements(By.XPATH, xpath_first_table_sql_rows)
for match in matches:
sql_server_a_element = match.find_element(By.XPATH, "./td/a[2]")
print(sql_server_a_element.text)
sql_server_rtm_version_a_element = match.find_element(By.XPATH, ".//td[#class='rtm']")
print('RTMs:')
print(sql_server_rtm_version_a_element.text)
if(show_service_pack_versions):
print('SPs:')
sql_server_sp_version_td_elements = match.find_elements(By.XPATH, ".//td[#class='sp']")
for td in sql_server_sp_version_td_elements:
print('---')
print(td.text)
print('----------------------------------')
if you set show_service_pack_versions = False then information regarding service packs will be skipped
There was a part of your code where you were calling b.text after getting the result of find_elements, which returns a list. You can only call b.text on a single WebElement (not a list of them). Here's the updated code:
from selenium.webdriver.common.by import By
from selenium import webdriver
website = "https://www.sqlserverversions.com"
driver = webdriver.Chrome(executable_path='/Users//Downloads/chromedriver/chromedriver.exe')
driver.get(website)
matches = driver.find_elements("css selector", "tr")
for match in matches[1:]:
items = match.find_elements("css selector", "td")
for item in items:
print(item.text)
That will print out A LOT of rows, unless you limit the loop.
If you just need text it's simpler to do it on the browser side:
data = driver.execute_script("""
return [...document.querySelectorAll('tr')].map(tr => [...tr.querySelectorAll('td')].map(td => td.innerText))
""")
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)
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)
I am trying to scrape the Google News page in the following way:
from selenium import webdriver
import time
from pprint import pprint
base_url = 'https://www.google.com/'
driver = webdriver.Chrome('/home/vincent/wintergreen/chromedriver') ## change here to your location of the chromedriver
driver.implicitly_wait(30)
driver.get(base_url)
input = driver.find_element_by_id('lst-ib')
input.send_keys("brexit key dates timetable schedule briefing")
click = driver.find_element_by_name('btnK')
click.click()
news = driver.find_element_by_link_text('News')
news.click()
tools = driver.find_element_by_link_text('Tools')
tools.click()
time.sleep(1)
recent = driver.find_element_by_css_selector('div.hdtb-mn-hd[aria-label=Recent]')
recent.click()
# custom = driver.find_element_by_link_text('Custom range...')
custom = driver.find_element_by_css_selector('li#cdr_opt span')
custom.click()
from_ = driver.find_element_by_css_selector('input#cdr_min')
from_.send_keys("9/1/2018")
to_ = driver.find_element_by_css_selector('input#cdr_max')
to_.send_keys("9/2/2018")
time.sleep(1)
go_ = driver.find_element_by_css_selector('form input[type="submit"]')
print(go_)
pprint(dir(go_))
pprint(go_.__dict__)
go_.click()
This script manage to enter search terms, switch to the news tab, open the custom time period tab, fill in start and end date, but fails to click on the 'Go' button after that point.
From the print and pprint statement at the end of the script, I can deduct that it does find the 'go' button succesfully, but is somehow unable to click on it. The error displays as selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
Could anyone experienced with Selenium have a quick run at it and give me hints as why it returns such error?
Thx!
Evaluating the css using developer tools in chrome yields 4 elements.
Click here for the image
use the following css instead:
go_ = driver.find_element_by_css_selector('#cdr_frm > input.ksb.mini.cdr_go')
I'm trying to create a simple login into a "Kahoot!" quiz .
First thing i'm trying to do is load from "https://kahoot.it/#/" JSON objects so i could fill the form in it (i tried to fill the form using 'mechenize' but its seems to support only html forms).
when im running the next script im getting exception that json could not be decoded:
import urllib, json
url = "https://kahoot.it/#/"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data
output:
ValueError: No JSON object could be decoded
Any ideas? ,
Thanks.
type(response.read()) is str, representing the HTML of the page. Obviously it's not a valid JSON therefore you are getting that error.
EDIT If you are trying to login to that page, it is possible with selenium:
from selenium import webdriver
url = "https://kahoot.it/#/"
driver = webdriver.Chrome() # or webdriver.Firefox()
driver.get(url)
# finding the text field and 'typing' the game pin
driver.find_element_by_xpath('//*[#id="inputSession"]').send_keys('your_game_pin')
# finding and clicking the sumbit button
driver.find_element_by_xpath('/html/body/div[3]/div/div/div/form/button').click()