How to verify an option is selected with Selenium with Python - python

I have a Select tag with several options (pictured below). I want to write a selenium test where I verify that the option with the 18 value is selected.
I have tried this but it doesn't work:
`age_min = self.browser.find_element_by_css_selector('select#age-min > option[value="18"]')
age_min_selected = is_attribtue_present(age_min, 'selected')
self.assertTrue(age_min_selected)`
I get this error: NameError: name 'is_attribtue_present' is not defined
I've also tried this:
`age_min = self.browser.find_element_by_css_selector('select#age-min > option[value="18"]["selected"]')`
error: Message: invalid selector: An invalid or illegal selector was specified

You could check the "selected" attribute on the option as such:
options = driver.find_elements_by_xpath("//option")
for option in options:
is_selected = option.is_selected()
print(str(is_selected))
Based on this documentation:
https://selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html?highlight=is_selected#selenium.webdriver.remote.webelement.WebElement.is_selected

Related

SyntaxError: Failed to execute 'evaluate' on 'Document': xpath

Element:
XPATH:
//*[#id="autocomplete_code"]
I am getting syntax error for below statement, what is not correct in this xpath? I tried just with id and both id & class.
account = driver.find_element(By.XPATH,"//*input[#id='autocomplete_code' and #class = 'account_code_ac inputSearch ui-autocomplete-input']")
changing code as below fixed the syntax error, but still not able to select the search result value with arrow down key
account = driver.find_element(By.XPATH,"//*[#id='autocomplete_code']")
account.send_keys("1100-1200-200167-620038")
account.send_keys(Keys.ARROW_DOWN)
account.send_keys(Keys.RETURN)
changing code as below works fine:
account =driver.find_element(By.XPATH,"//*[#aria-label='Search an Account']").click()
account = driver.find_element(By.XPATH,"//*[#id='autocomplete_code']")
account.send_keys("1100-1200-200167-620038")
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//a[contains(#aria-label, "1100-1200-200167-620038")]')))
account.send_keys(Keys.ARROW_DOWN)
account.send_keys(Keys.RETURN)

I want to write xpath for the dropdown list and select all sesason's in python Selenium

I have tried this to navigate from the season's drop down.
try:
add = driver.find_element_by_xpath(//ul[#class='dropdown dropdown__filter']/li[#class='pv-h'])
except:
print("Error on dropdown")
try:
Hover = ActionChains(driver).move_to_element(add).perform()
data=driver.find_element_by_by_xpath(/ul[#class='dropdown dropdown__filter']/li[#class='pv-h'])
time.sleep(2)
Error 1: add = driver.find_element_by_xpath('//ul[#class='dropdown dropdown__filter']/li[#class='pv-h']')
Error 2: Deprecation error: try find_elements_by()
It won't work because selenium does not allow to find elements by compound classes, ie. class attribute that has more than one value.
Try this:
try:
add = driver.find_element_by_xpath(//ul[contains(#class='dropdown__filter')]/li[#class='pv-h'])
except:
print("Error on dropdown")

Unable to access "from" property of a JIRA item

I am trying to access the from property of an item while accessing JIRA. Python gives me a syntax error. What could be the problem?
for history in changelog.histories:
for item in history.items:
if (item.field.upper() == 'SPRINT'):
try:
if item.fromString:
sFromSprint = str(100) #python does not like item.from
else:
sFromSprint = str(iZERO)
Error:
iFromSprint = item.from
^
SyntaxError: invalid syntax
from is a reserved keyword in python
You can get the property using getattr(item,'from') instead.

How can I click in link but without "a href" [Selenium + Python]

I don't know how can I click on "Utwórz konto" on https://www.morele.net/login.
I tried:
link_registration = driver.find_element_by_class_name("//li[#class = 'el-login-nav register']")
link_registration.click()
but I get error:
selenium.common.exceptions.InvalidSelectorException: Message: Given
css selector expression ".//li[#class = 'el-login-nav register']" is
invalid: InvalidSelectorError: './/li[#class = 'el-login-nav
register']' is not a valid selector: ".//li[#class = 'el-login-nav
register']"
You are trying to pass an XPath expression to the "by class name" locator - no wonder it fails.
I would use a CSS selector instead:
register = driver.find_element_by_css_selector(".autorization-form .register")
register.click()
.autorization-form .register would match an element having a register class somewhere (at any depth level) inside an element having an autorization-form class. Space here means parent-child relationship - but the child can be at any level inside the parent.
You are getting that error because you used the find_element_by_class_name() command but passed in an xpath. You need to either use find_elements_by_xpath() or a different locator function. Either of these should work:
driver.find_element_by_xpath("//li[#class = 'el-login-nav register']");
driver.find_element_by_css_selector("li.register")

How to get rid of Cannot focus element exception

I faced with following issue with chromedriver: I have a text input field and a texarea. I can successfully send text to both elements with follow code
input = driver.find_element_by_xpath('//input[#type="text"]')
input.send_keys('test')
textarea = driver.find_element_by_xpath('//textarea[not(#readonly)]')
textarea.send_keys('test')
But if to try this code
text_fields = driver.find_elements_by_xpath('//*[input[#type="text"] or textarea[not(#readonly)]]')
for field in text_fields:
field.send_keys('test')
I get selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
P.S. Adding field.click() before sending text or using ActionChains failed to solve issue. Also len(text_fields) return 2, so both elements correctly matched with XPath
The second expression will return the parent element of the input or textarea. If you wish to get both in a single XPath then:
text_fields = driver.find_elements_by_xpath("//input[#type='text'] | //textarea[not(#readonly)]")
for field in text_fields:
field.send_keys('test')
Or with a CSS selector :
text_fields = driver.find_elements_by_css_selector("input[type='text'] , textarea:not([readonly])")
for field in text_fields:
field.send_keys('test')

Categories

Resources