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")
Related
I am trying to get the content of the element source of one page
from selenium import web driver
firefox = webdriver.Firefox()
firefox.get('some link right here')
linkimg = firefox.find_elements_by_xpath('/html/body/div[3]/div/div/img').get_attribute('src')
but when i run this code it says: AttributeError: 'list' object has no attribute 'get_attribute'
Im tried on python 3.7 and 3.9
The output of .find_elements_by_xpath() is always a list of the found elements. Calling .get_attribute() does not make sense for a list. Instead, iterate over the list and get your src attributes instead.
linkimg = firefox.find_elements_by_xpath('/html/body/div[3]/div/div/img')
linkimg = [each_element.get_attribute('src') for each_element in linkimg]
I am trying to write custom function using robot framework existing Seleniumlibrary in python to get link from element. But I am keep getting an issue in get_attribute.
Error : 'list' object has no attribute 'get_attribute'
Library imported
from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn
def get_one_links(locator,attribute):
lib = BuiltIn().get_library_instance('SeleniumLibrary')
links = lib.find_elements(locator).get_attribute(attribute)
return links
That's because you are trying to call the method on a list, you can only call on a single element. See the example below. The "get_attribute" property doesn't exist for lists, but the "get_attribute" property does for single element. For example:
You need to do something like this in your code,
from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn
def get_one_links(locator,attribute):
lib = BuiltIn().get_library_instance('SeleniumLibrary')
links = lib.find_elements(locator)
for link in links:
return link.get_attribute('href')
#return link if thats what you want
from selenium import webdriver from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\\Users\\rahuls1\\chromedriver.exe")
driver.get('https://www.pastemagazine.com/')
elem = driver.find_element_by_id = ('master-search')
elem.send_keys('music')
elem.send_keys(Keys.RETURN)
time.sleep(5)
Error
File "C:\Users\rahuls1\Desktop\PYT\cl.py", line 9, in <module> elem.send_keys("music",) AttributeError: 'str' object has no attribute 'send_keys'
You are assigning 'master-search' to elem, not a web element. The correct way to use find_element_by_id is
elem = driver.find_element_by_id('master-search')
Your find element method is wrong in your code.You are find element method is wrong There are multiple locators available in the selenium which identifies element on the web.
ID
Name
Link Text
CSS Selector
XPath
You can also try below xpath:
driver.find_element_by_xpath("//input[#id='master-search']").send_keys("musics")
Please find below link which will let you know how to deal with web elements for more information.
https://selenium-python.readthedocs.io/locating-elements.html
I want to get information from table at the page https://www.oddsportal.com/soccer/england/premier-league/wolves-newcastle-utd-nNNqedbR/ .
This is a table, which automatically change her items(mb with js, ajax).
If i write following code, I get error 'HtmlElement' object has no attribute 'find_element_by_xpath'
url = 'https://www.oddsportal.com/soccer/england/premier-league/wolves-newcastle-utd-nNNqedbR/'
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(chrome_options=options)
driver.get(url)
html = lxml.html.fromstring(driver.page_source)
tbody = html.find_element_by_xpath('//*[#id="odds-data-table"]/div[1]/table/tbody')
trows = tbody.find_elements_by_tag_name("tr")
lxml is (presumably) the lxml library, so your html object is an instance of it. As the exception says - it does not have find_element_by_xpath() and tag_name methods, those are in the selenium library.
So instead of working with the html object, work with driver:
tbody = driver.find_element_by_xpath('//*[#id="odds-data-table"]/div[1]/table/tbody')
trows = tbody.find_element_by_tag_name("tr")
I am new to python. I have a code in R that I am trying to replace with a python
script. I am running into issues getting python to select a value from a drop
down menu.
This is the code in R that worked:
remDr$findElement(using = 'xpath', "//select[#id = 'groupby1']/option[#value = 'ReportDate']")$clickElement()
This is the HTML code:
select style="" class="dropdown" name="groupby1" id="groupby1" accesskey="" waffle_affected_fields=""
option value="ReportData">Report Date</option>
here are a couple things I tried after searching how to do this in python and I
keep running into errors.
find_element_by_xpath("//select[#id='groupby1']/option[#value='ReportDate']").click()
NameError: name 'find_element_by_xpath' is not defined
Select(driver.find_element_by_css_selector("select#groupby1")).select_by_value('ReportDate').click()
NameError: name 'Select' is not defined
Any help is appropriated!
Select doesn't have click(). Use it like this
Select(driver.find_element_by_id('groupby1')).select_by_value('ReportDate')
# or by text
Select(driver.find_element_by_id('groupby1')).select_by_visible_text('ReportDate')
These functions are properties of your webdriver instance. You need to do something like this:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
driver.find_element_by_xpath("//select[#id='groupby1']/option[#value='ReportDate']").click()
See the getting started page for examples.