Selenium Python -- Message: element not visible - python

I am trying to click on a href link.
I managed to get the link by
element = browser.find_element_by_xpath("//a[contains(#href,'main.cfm')]")
But when I was usingelement.click(), it showed
Message: element not visible
I am not sure why. Could it be the "==$0" in the picture? When I moved the mouse on to the text it says "Use $0 in the console to refer to this element".

Answer of #Shoaib Akhtar is right, You can also use this xpaths
//span[contains(text(),'Latest Projects')]
//span[contains(#class,'uppercase')][contains(text(),'Latest Projects')]

Try this xpath
//a[span[text()[contains(., 'Latest Projects')]]]
this is to select parent link of 'span' element which has substring text 'Latest Projects'.

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

Selenium unable to find Button

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.

Click on li element searching specific displayed text

Using python 3 and chrome driver. I'm trying to click on my desired element searching for the text displayed on this page . For example, in case of "BEBES" I'm using:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[contains(text(), "BEBES")]'))).click()
but nothing happens. Just throws the time out exception. What's my error?
Your xPath is not correct. Use this:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[contains(text(), "Bebes")]'))).click()
Note: upper/lowercase makes difference
and
This post suggests using the following as text() returns a node set:
//*[text()[contains(.,'BEBES')]]
XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

Python, selenium Can't hook following-sibling element as xpath

I have following html code:
I need to hook the "edit" icon for particular element which has title "test".
I tried to do
//*[contains(text(), 'test')]/following-sibling::div/span[#title='Edit']
But it doesn't work.
I can't identify this element by just title = Edit, because there are a lot of elements which will have this button edit. The only unique this is first title "test" and following sibling "Edit".
Any ideas? Would appreciate any help.Thank you
You can select the div with title="test", then get the span with title="Edit" like this:
xpath("//div[#title='test']/following-sibling::div/span[#title='Edit']")
or directly select the div with span title='Edit':
xpath("//div/span[#title='Edit']")
Was able to locate this element with following path:
//*[contains(., 'test')]/following-sibling::div/span[#title='Edit']
Thank you everyone for your help.

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