Node.js Selenium can't find element. No such element error - node.js

So i have this element:
and i just can't find it, tried
driver.findElement(webdriver.By.partialLinkText('my_sites')).click();
but that just throws no such element error

I believe selecting by partial link text searches the visible text (the text in between the opening and closing a tag) rather than the href. Since you have no text within the a tag, it is not finding it. You would have to find using xpath something like "//a[contains(href, 'my_sites')]"

For selenium link text is what you find between the HTML brackets, for example:
LinkText
You can try to select via CSS selector:
driver.findElement(By.cssSelector("a[href*='my_sites']")).click();
Check this link for more info:
How to click a link whose href has a certain substring in Selenium?

Related

How do I get the class name from HTML using the absolute xpath

In the following picture, the full xpath of the yellow highlighted bit of HTML is
/html/body/bx-site/ng-component/div/sp-sports-ui/div/main/div/section/main/sp-path-event/div/sp-next-events/div/div/div[2]/div1/sp-coupon1/sp-multi-markets/section/section/sp-outcomes/sp-two-way-vertical[2]/ul/li1/sp-outcome/button
I am using selenium to scrape some data from a website. The text of the xpath is what I want but I also need the class name of the yellow highlight bit of HTML. The class name constantly changes so I need a way to retrieve the class name along with the text. In this case the class name would be "bet-btn". I am using driver.find_element_by_xpath to get the text from the html, but can't figure out a way to retrieve the class name. Using the xpath is there a way in selenium to retrieve the class name of the yellow highlighted bit.
I would advise against using absolute xpath unless you really needed to
Try this instead:
elem = driver.find_element_by_xpath("//sp-outcome/button")
class_value = elem.get_attribute("class")
BTW that xpath is assuming there are no other //sp-outcome/button element paths on that page. If there are you would need to expand it some, but you still wouldn't need the entire absolute xpath. Those are generally pretty fragile.

Selenium: failing to find element by XPATH Python

I am a little bit new to programming but python really made get into it. I am trying to create a programm that automatically checks for updates in a website. I've successfully implemented the neccessary code to call the page of enrollment but yet there is one element that cannot be located. Since I have to do it for multiple courses and iterate throught them there is no specific id, I've tried to find it by title but also this didn't work.
Is there a way you can locate the button with the title "enroll".
I've tried
driver.find_element_by_xpath("//a\[#title ='enroll']").click()
but this didn't work and I always get
NoSuchElement
error.
The XPATH for the button is simply: //*\[#id="id572"\]
Here is the part of the HTML code:
From the screenshot of HTML code you provided, the element is <button>, not <a>.
Try this xpath expression //button[#title='enroll']
This should do it if it's not in any iframes. Just grab the button whose title is enroll and click. Your css selector was an a tag and it might get id dynamically.
driver.find_element_by_css_selector("button[title ='enroll']").click()

Sending text to text box using Selenium in Python

I am trying to create a script within Python using Selenium to open a social media page (Tumblr), input user login credentials, and make "quote" posts with a random sentence generator. I am able to input the credentials and select the create post icon using xpaths (driver.find_element_by_xpath) from the website, but when trying input text for the post with the xpath
//*[#id="redpop_iframePostForms"]/div[3]/div/div/div/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div/div[1]/p
using the
self.driver.find_element_by_xpath('//*[#id="redpop_iframePostForms"]/div[3]/div/div/div/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div/div[1]/p').send_keys("It worked!!")
I receive the error
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#id="redpop_iframePostForms"]/div[3]/div/div/div/div/div[2]/div[2]/div/div[2]/div/div[1]/div/div/div[1]"}
I have tried using the other div classes within the list of divs, but I cannot find the correct one. I have also tried using the CSS selector with the code
driver.find_elements_by_css_seletor("div[#aria-label='Quote']").send_keys("It worked!!!")
but that provided the error
Message: invalid selector: An invalid or illegal selector was specified
Any direction on where to go from here?
Thanks!
For no such element exception
From your xpath, i can guess that element is present in an iframe. First switch to that frame and then find element you want to interact with.
For invalid selector message
General Syntax for locating element with css selector is :
tagname[attributeName=‘attributeValue’]
You don’t need to use # with attributeName.
Also, you are fetching all the div elements present in the DOM with aria-label=‘Quote’ and sending keys which is not right. If you want to perform any action on elements, you need to put them in list and then iterate over it. Hope this helps.

Performing search opertaion while using chrome but not in firefox(Selenium)

This is the code snippet for chroome:
driver.get("https://www.youtube.com/")
driver.find_element_by_xpath('//*[#id="search"]').send_keys("kao")
driver.find_element_by_xpath('//*[#id="search-icon-legacy"]').click()
The browser automatically opens Youtube and searches for the passed string[which is the task I intend to do]
The main problem appears when i start using firefox.The page loads properly.I am basically using the same code but every time I run it, it throws the following error:
Message: Element <g id="search"> is not reachable
I cannot pass any string to the search bar or even click it.
There is a slight error in the xpath //*[#id='search'] that it returns two elements as in below screen. The reason being the * in the above xpath acts as a wild card and matches the other ytd-searchbox<id="search"> tag in the page. The same is true for //*[#id="search-icon-legacy"].
So you must try changing to the xpath like below to locate the web elements uniquely
//input[#id='search']
//button[#id='search-icon-legacy']
Errored Xpath:
Your Xpath is not correct. It's Unable to locate search field and buttton
You can use this one
driver.get("https://www.youtube.com/")
driver.find_element_by_xpath("//input[#id='search']").send_keys("kao")
driver.find_element_by_xpath("//button[#id='search-icon-legacy']").click()

Tool to find xpath and iframe id

I'm automating a web page which contains several nested iframes. This makes it hard for me to find the proper locator for the elements. I use firebug, the f12 tools ... to get xpath etc. but I miss something here, the iframe id.
Does someone know a tool where I can point on an element on the web page, and get the xpath and the iframe id?
THX
Firebug displays the iframes containing the inspected element within its ancestor path:
By clicking on the buttons within the ancestor path you can jump to the related iframe.
To get the XPath for an iframe right-click on it within the HTML panel and choose Copy Minimal XPath or Copy XPath from the context menu.

Resources