Selenium unable to find Button - python

I am trying to click on the "Next Page"-Button on the Web of Science Search-Site to iterate through all pages.
Here is a screenshot of the HTML of the page (highlighted is the button)
This is my code to find the button:
driver.find_element_by_class_name('mat-focus-indicator mat-icon-button mat-button-base').click()
But I receive this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".mat-focus-indicator mat-icon-button mat-button-base"}
I have tried so many ways of identifying the button (find_by_id, find_by_name, find_by_link_text) but nothing works.
What am I doing wrong?
Thank you in advance

Maybe try with Query Selector like:
driver.find_element_by_css_selector('button[data-ta="next-page-button"]')
You can always try selectors on elemnts panel (like on screenshort) and type your selector in field "Find by string, css, or xpath"

Class name do not have support for spaces, remove spaces and put . to make it css selector :
driver.find_element_by_css_selector('button.mat-focus-indicator.mat-icon-button.mat-button-base').click()
also try to put some wait before this to make it more consistent.

Related

Selenium Not Finding by ID, Class, or anything else... No iframes appear to be present

I've been looking through questions and answers like this one: Selenium Unable to Locate Element by ID
Most elements appear inaccessible by ID or XPATH. I'm attempting to find and click the element that has the text "Add Parent":
I've tried things like:
browser.find_element(By.XPATH, "/html/body/div/main/fs-person-page//iron-pages/fs-tree-person-details//div/section/fs-tree-person-family//fs-tree-collapsable-card/fs-family-members//div[2]/section[2]/div/button[2]/span/fs-inner-html")
and
browser.find_element(By.XPATH, "//fs-inner-html[text() = 'Add Parent']")
(similarly, finding by ids and classes doesn't seem to be working)
They solve the solution of not being able to find an element by switching to the iframe in which the element resides. The web page in which I'm searching for elements doesn't have any iframes. Do I need to be switching to something else? How can I determine what frame-like element I should be switching to?
Thanks!
Anson
The HTML of the webpage that I'm trying to scrape can be found here.
The issue with the
browser.find_element(By.XPATH, "//fs-inner-html[text() = 'Add Parent']")
xpath is that the text probably doesn't exactly equal 'Add Parent'.
There may be white-space on either side or the inside span 'to Maria Danko' is causing it to fail.
Just as a sanity check could you seen if any of these work?
button.add-person //css selector
button.add-person[data-test-add-parent=''] //css selector
//fs-inner-html[contains(text(), 'Add Parent')] //xpath

Unable to locate element _ Python Selenium

I'm trying to locate HTML element with python selenium but I'm always getting the following message : Unable to locate element.
Code I'm using : browser.find_element_by_id("X14Edit")
HTML code is below :
Thanks in Advance.
Meriem.
You need to switch to the iframe before accessing that element.
I hope the iframe id is not dynamic.
In this case your code will be:
browser.switch_to.frame(driver.find_element_by_id("ext-gen341"))
browser.find_element_by_id("X14Edit")
If the iframe id is dynamic, but there is no more iframes there you can simply select it by tag name as following:
browser.switch_to.frame(driver.find_element_by_tag_name("iframe"))
browser.find_element_by_id("X14Edit")

How to find the xpath in selenium

I need to find the xpath for the line highlighted in the image; the page is dynamic.
I tried this:
//td[contains(text(),'Ricardo')]
But gave me an error
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//td[contains(text(),'Ricardo')]"}
You may try:
driver.findElement(By.xpath("//*[text()='Ricardo']"))
I assume you know how to inspect the elements on a page. In the console window, you can then just right click the element you want and copy the Xpath. Once you've copied it, paste in following code: driver.find_element_by_xpath("paste here"). You might see that within the xpath there are double brackets and you will need to replace these by single brackets.

How to obtain required content from "find_element_by_xpath"

I'm trying to use selenium & python to auto get part of the html content.
I use find xpath like below ways to sort out the price from specified flight number, but always get failed result " unable to locate "
Anyone could shed some lights on it ?
element_price = driver.find_element_by_xpath("//div[#id='flight_MU5401']")
element_price.find_element_by_xpath(".//span[#class='base_price02']")
It's the html
If I were to guess, I would say that you are probably getting that error because the element isn't loaded when your code runs. It's probably a slower loading page. You should try adding a wait and see if that helps.
You can also simplify your locator by using a CSS selector and only scraping the page once.
price = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#flight_MU5401 span.base_price02")).text

Using python selenium to click an element not visible

I am using python selenium on browser to fill in some form. I was trying to select an element in the drop-down list,
0
but if I try to find it by text using this script:
browser.find_element_by_link_text("0").click()
it result an error:
"unknown error: Element is not clickable at point (498, 612). Other element would receive the click: ..."
and if try to find it by class name:
browser.find_element_by_class_name("dropdown-toggle").click()
it result in another error: "element not visible"
is there any way I can click onto that drop-down list? Thanks very much.
I had a similar problem. You can execute a script to change the visibility of that element once you find it and then click it.
driver.execute_script("arguments[0].style.visibility = 'visible';",myElement)
myElement.click()
Try to find it by Xpath searching for partial class and text at the same time:
browser.find_element_by_xpath(//a[contains(#class, 'dropdown-toggle select') and contains(text(), '0')]).click();
you should get the element by xpath and then press it.
browser.find_element_by_xpath(xpath).
read here how to get the xpath: http://www.wikihow.com/Find-XPath-Using-Firebug
Thanks for the feedback, I've found the problem which was due to the new script being loaded by javascript as triggered by some clicks. I've created the following code to capture the exception and retrying until the element is ready:
while True:
try:
time.sleep(1)
browser.find_element_by_xpath("//div[#class='tckt']/a").click()
print("found")
break
except ElementNotVisibleException:
print("notvisible")
except WebDriverException:
print("clickduplicate")
I read it on articles says this happened a lot on radio button for Chrome webdriver, hope this helps.
Firstly click the parent element by finding it, using it's xpath->find the element you want to click within the parent element like the way you did. i.e
browser.find_element_by_link_text("0").click()
Hope this 2 steps will work for you. Otherwise please post the url with the code you've tried-It'll be easy to find out the issue.

Categories