Selenium: failing to find element by XPATH Python - python-3.x

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()

Related

Getting error Element not interactable on trying to send Page Down keys to div

I am trying to scroll Telegram using selenium in python. In the attached screenshot I have shared I have selected 'Members' as the element to send Keys.PAGE_DOWN as it is stick all the time to top and is static while scrolling so it should be visible all the time and can be the perfect element to send Keys.PAGE_DOWN to.
But on sending page_down I get error 'Element not Interactable'.
Any suggestions what I am doing wrong?
I have attached the script and screenshot.
I am using python 3.10 and selenium latest version.
`driver.find_element(By.XPATH, "//*[#id='RightColumn']/div[2]/div/div/div[2]/div[2]/div[1]").send_keys(Keys.PAGE_DOWN)`
I have tried all the answers currently available on the internet and they don't work here. This looks like some complex issue.
I think Selenium is throwing the right error message as this div is not an interactable element and you are trying to send keystrokes into the element.
Another approach for scrolling is using Javascript commands.
Find an element locator you need to scroll to.
(Ex: if you need to scroll to the bottom find the element at the bottom)
Use the below code to scroll
# Find the element in the page to scroll to
element = driver.find_element_by_xpath("//element/at/bottom/of/the/page")
# Fire javascript command to scroll in to view
driver.execute_script("arguments[0].scrollIntoView();", element)

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()

How do I interact with a dynamic id through python selenium?

I am trying to click on an icon in a web page. This is the element I am attempting to click:
<a class="Button ButtonIcon IconOnly DataSelector NormalState"
id="ze6402ef81ea54445aec5dab8790c781f" tabindex="0"><span class="Icon"></span>
<span class="Text"></span></a>
I have no problem interacting with the code below:
browser.find_element_by_css_selector('ze6402ef81ea54445aec5dab8790c781f').click()
The problem is that the id is dynamic with each session. I have attempted a workaround with the following code with no success:
browser.find_element_by_xpath("//a[span/#class='Text']").click()
and
browser.find_element_by_xpath("//a[span/#class='Icon']").click()
Afterwards, I noticed that the element needs to be in a hover state in order to be clicked. So next, I used ActionChains to try to simulate a hover state -- again, with no success:
actions=ActionChains(browser)
element=browser.find_element_by_css_selector("//a[span/#class='Icon']")
actions.move_to_element(element).click().perform()
Then, I tried to TAB to the element via send_keys and ActionChains -- but it ended up cycling rapidly through page, instead of one element at a time:
actions.send_keys(Keys.TAB)
I wanted to put in my due diligence before posting my issue. Any assistance is appreciated - Thank you.
As you mentioned, you don't have a problem with the following line of code:
browser.find_element_by_css_selector('ze6402ef81ea54445aec5dab8790c781f').click()
But the only issue here is that the id is dynamic, so we can use the class attribute to construct an unique cssSelector or an unique xpath as follows:
cssSelector :
driver.findElement(By.cssSelector("div.Button.ButtonIcon.IconOnly.DataSelector.NormalState"));
xpath :
driver.findElement(By.xpath("//div[#class='Button ButtonIcon IconOnly DataSelector NormalState']"));
Use these XPaths:
//span[#class='Text']
//span[#class='Icon']
Yours were formatted incorrectly.

Finding a dymanic element in Selenium: "element not visible" error

I am trying to automate a process using Selenium. I almost always use:
driver.find_element_by_xpath('xpath')
to locate the needed elements. As I go through the process, I end up clicking an element that opens a dialog box as such:
http://imgur.com/a/NUoIM
The element I am trying to click on looks like this:
http://imgur.com/1Zle5zG
The problem is that the xpath and the id are both dynamic, so each time I create a new session, I am dealing with slightly different information.
I located the element by using the following:
driver.find_element_by_xpath('//div[contains(#id, "56$187009")]/div[contains(#class, "gwt")]')
But when I try and send_keys to the element, I get the following error:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
I can see the element on the screen but it seems the driver can not find it. I tried to use:
driver.switch_to_alert()
Try out the following line of code:
driver.find_element_by_xpath("//div[starts-with(#id, '56')][#class='WN5Q WCAR WCU']/input[contains(#class, 'gwt-TextBox WO5Q WBAR')]")

Resources