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

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)

Related

Double condition with Xpath selenium Python

I want to access elements of all balises that find like this : <div class='xxxx' the text >
I did as this with Xpath:
textSearch = 'the text'
hometeam = driver.find_elements(By.XPATH,"//div[#class='xxxx'] and //div[contains(text(),'"+ textSearch +"')]")
But this not works and casting this exception : TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.
How do ?
Combine the conditions within the [...]
E.g., //div[#class='xxxx' and contains(text(), 'justin')].
Demo here

How to verify an option is selected with Selenium with 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

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 check if element exist in lxml xpath?

I use lxml xpath for parsing HTML page in Python 3.
As sample I have code, that finds element HTML:
version_android = doc.xpath("//div[#itemprop='operatingSystems']//text()")
Father I have insert Mysql query:
insert = ("insert into tracks (version) values ('%s')" % (version_android[0]))
Problem is, that if is not element in HTML DOM, therefore I get Mysql error when I try to get parsed result in line: version[0] and put in query.
Sometimes result array does not have index version_android[0], but has index version_android[2]: It makes error in insert function Mysql.
How can I validate this correct? I have a lot the same rules for parsing.
I tried this, but I dislike this solution:
version_android = doc.xpath("//div[#itemprop='operatingSystems']//text()")
if len(version_android):
version_android = version_android[0]
else:
version_android = ""
I think the better way (In my opinion) is using the except.
#valid_xpath = '__VIEWSTATEGENERATOR'
invalid_xpath = 'XXXXXXXX'
try:
vgenerator = root.xpath('//*[#id="'+ invalid_xpath +'"]//#value')[0]
except IndexError:
vgenerator = None
print vgenerator

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