Unable to find Input value using Xpath - python

New to python here. I am trying to have my script click a checkbox with a given input value. Unfortunately there is no input ID. Using find_element returns with error:
Unable to locate element
Using find_elements() returns a list and therefore I get the error:
'list' object has no attribute 'click'
This is the code I have had trouble with.
OldJob = driver.find_elements_by_xpath("//input[#value='0 ']")
OldJob.click()
Here is the html:
Any thoughts on how I can get around this?

To locate the <input> element you can use either of the following Locator Strategies as follows:
cssSelector:
driver.find_element_by_css_selector("input[name='RowNum'][value^='0'][type='checkbox']")
xpath:
driver.find_element_by_xpath("//input[#name='RowNum' and contains(#value, '0')][#type='checkbox']")

Related

Python Selenium: Selecting from a list by text

I have spent the past few days trying to solve this problem. I store a string in a variable and i am trying to automate using selenium to select the item whose text()='string stored in the variable'.
I noticed i could not select some of the items in the list even though the text was exactly the same as the string i stored in the variable. The i realized today that some of the texts have a space before the next item. For example;
instead of (Architecture)
it says (Architecture )
This is the code i was running;
fc = 'COMMUNICATION'
faculty = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="agentApplicationForm'
'"]/div/div[9]/div['
'1]/div/span/span/span[1]')))
print(faculty.is_displayed())
faculty.click()
sleep(5)
faculty_select = driver.find_element_by_xpath(f'//*[#id="Program1FacultyInstitute_listbox"]/li[text().StartsWith("{fc}")]')
faculty_select.click()
sleep(2)
I am now trying to use text().startsWith({fc}) but i don't think i am using a correct syntax.
This is the error i get;
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[#id="Program1FacultyInstitute_listbox"]/li[text().startsWith("COMMUNICATION")] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[#id="Program1FacultyInstitute_listbox"]/li[text().startsWith("COMMUNICATION")]' is not a valid XPath expression.
(Session info: chrome=88.0.4324.96)
Can someone help me please.
Thank you very much.
Try this to search text starting with:-
//*[#id="Program1FacultyInstitute_listbox"]/li[starts-with(text(),'fc')]
Or maybe try using contains to do substring match like below. But you will need to pass more accurate text to search in order to avoid multiple matches.
//*[#id="Program1FacultyInstitute_listbox"]/li[contains(text(),'fc')]
if you want to select only (one) element based on (text)
you can use
faculty.click()
faculty.send_keys("value you want to select")
notice that this method works only if you want to select only one value based on string , also it works in droplist.

Drop down list selenium python

Im trying to use a drop down list whit selenium :
browser.get('https://account.mail.ru/signup?from=main&rf=auth.mail.ru')
day = Select(browser.find_element_by_xpath('/html/body/div[1]/div[3]/div[3]/div/div/div/form/div[5]/div[2]/div/div[1]/div/div/select'))
dayDD=Select(day)
dayDD.select_by_index(3)
but i always get this error :
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[3]/div[3]/div/div/div/form/div[5]/div[2]/div/div[1]/div/div/select"}
I dont know why it can't find xpath since i get it same way i get all my other xpaths
could you attach DOM page screen with list of elements?
At this stage I see 2 posibilities:
- Elements are not loaded
Use implicit or explicit wait : https://www.geeksforgeeks.org/waits-in-selenium-python/?ref=rp
- Xpath is not correct
Maybe learn how to use relative path : https://www.tutorialspoint.com/xpath/xpath_relative_path.htm

Looping through webelements with selenium Python

I am currently trying to automate a process using Selenium with python, but I have hit a roadblock with it. The list is part of a list which is under a tree. I have identified the base of the tree with the following xpath
item = driver.find_element_by_xpath("//*[#id='filter']/ul/li[1]//ul//li")
items = item.find_elements_by_tag_name("li")
I am trying to Loop through the "items" section but need and click on anything with an "input" tag
for k in items:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((k.find_element(By.TAG_NAME, "input")))).click()
When execute the above I get the following error:
"TypeError: find_element() argument after * must be an iterable, not WebElement"
For some reason .click() will not work if I use something like the below.
k.find_element_by_tag_name("input").click()
it only works if i use the webdriverwait. I have had to use the web driver wait method anytime i needed to click something on the page.
My question is:
What is the syntax to replicate items = item.find_elements_by_tag_name("li")
for WebDriverWait(driver, 10).until(EC.element_to_be_clickable((k.find_element(By.TAG_NAME, "input")))).click()
i.e how do I use a base path and append to the using the private methods find_elements(By.TAG_NAME)
Thanks in advance
I have managed to find a work around and get Selenium to do what i need.
I had to call the javascript execution, so instead of trying to get
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((k.find_element(By.TAG_NAME, "input")))).click() to work, i just used
driver.execute_script("arguments[0].click();", k.find_element_by_tag_name("input"))
Its doing exactly what I needed it to do.

How do I click a button using Selenium in Python?

I'm trying to crawl review data on an app from a Google Play website page using Python and Selenium. What I'm trying to do here is to click the "Full Review" button to see the entire text of each review.
But I keep running into these errors each time:
ElementNotInteractableException: Message: element not interactable
or
AttributeError: 'list' object has no attribute 'click'
or
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".LkLjZd ScJHi OzU4dc "}
The element for the button is this:
<button class="LkLjZd ScJHi OzU4dc " jsaction="click:TiglPc" jsname="gxjVle">Full Review</button>
The xpath for the button is this:
//*[#id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button
And this is the code I'm using for xpath:
full_review = driver.find_elements_by_xpath('//*[#id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button')
full_review.click()
I can't find the button by class name, xpath or anything.
Could anyone help me figure out this problem? In addition, is it possible to find the element by jsname?
I would really appreciate if you could help.
Avoid using xpath whenever possible, it's the most brittle selector.
id > CSS > Xpath
For your button, this css selector should work:
button[jsname='gxjVle']
You'll probably need to specify the child as that probably won't be unique
Your XPath selector is a little bit flaky, you can simplify it to be something like:
//button[text()='Full Review']
You're using the wrong function, if you're looking for a single unique element you should be using WebDriver.find_element_by_xpath one, mind that element, not elements
It's better to use Explicit Wait just in case element is not immediately available in DOM (for example if you're testing application built using AJAX technology)
Assuming all above:
full_review = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Full Review']")))

Python Selenium CSS Selector by Span get_attribute

I have already accessed a webpage, populated text boxes and drop downs with required parameters, and submitted (button click) this information so the webpage can perform a calculation. I am trying to access the results (value of text). Results should be a list of calculations listed least to greatest, and I only want the lowest value. I am not sure if I am having a timing issue or a CSS Selector issue.
I have tried:
e = driver.find_elements_by_css_selector("span[data-bind='calc']")
new = e[0].text
print(new)
Error: IndexError: list index out of range
I wanted to make sure the data table was being completely populated before I tried to access it's calculated elements, and I have also tried:
output_by_css = WebDriverWait(driver, 10).until(
lambda driver : driver.find_elements_by_css_selector('span.calc')
)
for output in (output_by_css):
print(output.text)
Error: raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Can someone please help me determine if this is an issue with the CSS Selector by span or if it's a timing issue, or something else I have not yet thought of? How can I fix it?
You can try applying the following selector:
driver.find_elements_by_css_selector('span[data-bind*=calc]')
Here we are getting all the span tags having data-bind attribute having "calc" inside the value.

Categories

Resources