Click on Cancel Button in Print Document Message - python

I am trying to click on the cancel button which is in the print document in chrome. So here is the HTML
<cr-button class="cancel-button" aria-disabled="false" role="button" tabindex="0">
Cancel
</cr-button>
I have tried the following but did not work
driver.get(resume_url)
driver.maximize_window()
# Trial
driver.find_element_by_css_selector('cancel-button').click()
# Trial
driver.find_element_by_link_text('Cancel').click()
Nothing of these were successful and it returned the following error:
Message: no such element: Unable to locate element: {"method":"link text","selector":"Cancel"}
(Session info: chrome=89.0.4389.114)

Locating elements by link text only works for anchor elements.
https://selenium-python.readthedocs.io/locating-elements.html#locating-hyperlinks-by-link-text
To use the class name as a selector, you just append a period to the beginning. Does this work?
driver.find_element_by_css_selector('.cancel-button').click()

Related

Selenium: Unable to locate element by class and id

Trying to scrape a website, I created a loop and was able to locate all the elements. My problem is, that the next button id changes on every page. So I can not use the id as a locator.
This is the next button on page 1:
<a rel="nofollow" id="f_c7" href="#" class="nextLink jasty-link"></a>
And this is the next button on page 2:
<a rel="nofollow" id="f_c9" href="#" class="nextLink jasty-link"></a>
Idea:
next_button = browser.find_elements_by_class_name("nextLink jasty-link")
next_button.click
I get this error message:
Message: no such element: Unable to locate element
The problem here might be that there are two next buttons on the page.
So I tried to create a list but the list is empty.
next_buttons = browser.find_elements_by_class_name("nextLink jasty-link")
print(next_buttons)
Any idea on how to solve my problem? Would really appreciate it.
This is the website:
https://fazarchiv.faz.net/faz-portal/faz-archiv?q=Kryptow%C3%A4hrungen&source=&max=10&sort=&offset=0&_ts=1657629187558#hitlist
There are two issues in my opinion:
Depending from where you try to access the site there is a cookie banner that will get the click, so you may have to accept it first:
browser.find_element_by_class_name('cb-enable').click()
To locate a single element, one of the both next buttons, it doeas not matter, use browser.find_element() instead of browser.find_elements().
Selecting your element by multiple class names use xpath:
next_button = browser.find_element(By.XPATH, '//a[contains(#class, "nextLink jasty-link")]')
or css selectors:
next_button = browser.find_element(By.CSS_SELECTOR, '.nextLink.jasty-link')
Note: To avoid DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() import in addition from selenium.webdriver.common.by import By
You can't get elements by multiple class names. So, you can use find_elements_by_css_selector instead.
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
print(next_buttons)
You can then loop through the list and click the buttons:
next_buttons = browser.find_elements_by_css_selector(".nextLink.jasty-link")
for button in next_buttons:
button.click()
Try below xPath
//a[contains(#class, 'step jasty-link')]/following-sibling::a

Extract Text from CSS Property using Selenium Python

I have an input tag html element that Selenium Python fails to identify (not because of the wait). So on a web page with a form (name is Form1), I want to extract the text in one of the fields. This is the html element here when I inspect the elements on chrome:
Input Element:
<input name="txtSerialNo" type="text" readonly="readonly" id="txtSerialNo" class="tbFormRO" style="width:160px;position:absolute;left:90px;top:7px;text-align:center;">
The full xpath is this when I right-click on the element to copy the xpath: /html/body/form/div[9]/input[1]
The HTML Element
There isn't any text on it, so I tried the below and all did not work. I also tried the implicit wait and WebDriverWait. They are irrelevant and did not work.
driver.maximize_window()
driver.find_element_by_xpath('/html/body/form/div[9]/input[1]')
driver.find_element_by_id('txtSerialNo')
driver.find_element_by_name("txtSerialNo")
driver.find_element_by_xpath("//input[#id='txtSerialNo']")
It all returns error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="txtSerialNo"]"}
(Session info: chrome=91.0.4472.114)
My question is: I can see that when I inspect the element, the text I want to retrieve is in the Property tab, under input#txtSerialNo.tbFormRO
Under Property
I am using a for loop to gather all input element, but I don't know how to extract that "value" property under the "category" of input#txtSerialNo.tbFormRO in the property tab when I inspect the element. Sorry I don't have a solid CSS/HTML knowledge.
The Text I Want to Extract
I tried the below without success:
for inp in driver.find_elements_by_xpath('//form[#name="Form1"]//input'):
for k in inp.get_property('attributes')[0].keys():
print(inp.get_attribute(k))
for inp in driver.find_elements_by_xpath('//form[#name="Form1"]//input'):
print(inp.value_of_css_property('value'))
# get_property(input#txtSerialNo.tbFormRO.text)
# .get_attribute('text')
# .get_attribute("innerHTML")
# .get_attribute('value')
# .get_property('input#txtSerialNo.tbFormRO.value')
I think you are looking for .get_attribute(). Based on the image lets adjust the xpath to '//input[#name="txtSerialNo"]'
for inp in driver.find_elements_by_xpath('//input[#name="txtSerialNo"]'):
print(inp.get_attribute('value'))

Python selenium webdriver select href select partial text

I'm using Python 3.8.5 on Ubuntu 20.04. Using selenium webdriver on chrome, I want to download the attachment by specifying the licenceId number (1467250) which is included in this element:
<a xmlns="http://www.w3.org/1999/xhtml" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['myApplicationsResultsForm'],'myApplicationsResultsForm:searchResultsTable:0:j_id339:0:j_id340,myApplicationsResultsForm:searchResultsTable:0:j_id339:0:j_id340,licenceId,1467250,statusHistoryId,2600790,fileName,ROL_1467250_20200817-142839.pdf,attachmentType,ROL','');}return false" class="pageLink"><img src="/oplinc2/images/pdf.jpg" alt="ROL_1467250_20200817-142839.pdf" height="24" style="border-width: 0px;" title="ROL_1467250_20200817-142839.pdf" width="24" /></a>
I am able to download this link by clicking on the css_selector:
pdf = driver.find_element_by_css_selector('#myApplicationsResultsForm\:searchResultsTable\:0\:j_id335 > a')
pdf.click()
Am I able to use partial text within the element to locate and download attachment eg. licenceID, 1467250? There are many of these attachments. I tried the partial text example from the docs but this didn't work for me:
>>> driver.find_element_by_partial_link_text('1467250')
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"1467250"}
Edit
This question is similar to #Ajay link to this solution except this element has slightly different href. Still not sure how to access onclick
try this
links = driver.find_elements_by_partial_link_text('https://websites.com/activation.php?a=act')
for link in links:
print(link.get_attribute("href"))
driver.find_element_by_xpath('//*[contains(#onclick, "1467250")]')
Replacing the a with * finds the element.

Click on button element without id/name using Python and selenium webdriver

I’m using Python and selenium to scan a web page and I want to click on a button that looks like this:
Later>
which is defined as:
<div class=”later___16eg8 col-xs-3of12”>
<button class=”visible-xs-inline visible-sm-inline visible-md-inline bdp___2gfNX textButton___ETrkF” data-test=”later-small-button”> == $0
“Later”
<span aria-hidden=”true”>></span>
</button>
<button class=”visible-lg-inline visible-xl-inline bdp___2gfNX textButton___ETrkF” data-test=”later-large-button”>
“Later”
<span aria-hidden=”true”>></span>
</button>
</div>
I tried:
later = driver.find_element_by_xpath('//button[text()="Later"]')
later.click()
and while the first line finds the button (or part of it), the second generates the following error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I also tried:
later = driver.find_element_by_xpath('//button[text()="Later>"]')
and this generated the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[text()="Later>"]"}
Any ideas?
Thanks
Have u tried //div[#class='later___16eg8 col-xs-3of12']/button/button[text()='Later']

Selenium click like on twitter

What is the most recent and up to date way to click "like" on a tweet using selenium?
From the css, it looks like I need to click on this:
<div aria-haspopup="false" aria-label="131310 Likes. Like" role="button" data-focusable="true" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-11cpok1 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="like">
That is found on a profile page. My goal isn't to exactly like a tweet on a profile page but If I can figure this out, I'll be able to figure out what I actually want to do.
I have tried a bunch of ways but here are my most recent:
like_btn = (By.XPATH, ("//div[#data-testid='like', #class='css-18t94o4 css-1dbjc4n r-1777fci r-11cpok1 r-1ny4l3l r-bztko3 r-lrvibr']"))
like_btn = (By.XPATH, ("//div[#data-testid='like'"))
copy of Xpath:
like_btn = (By.XPATH, ("//*[#id='react-root']/div/div/div/main/div/div/div/div[1]/div/div[2]/div/div/div[2]/section/div/div/div/div[3]/div/article/div/div[2]/div[2]/div[4]/div[3]/div"))
Copy of full xpath:
like_btn = (By.XPATH, ("/html/body/div/div/div/div/main/div/div/div/div[1]/div/div[2]/div/div/div[2]/section/div/div/div/div[3]/div/article/div/div[2]/div[2]/div[4]/div[3]/div"))
The method then uses:
def like_user_tweets(self, user_tweets=UserStatuses.like_btn):
self.like_btn.click()
It looks like twitter recently revamped everything because even github code from only a year or less ago uses "HeartAnimation" class to find the like button but that doesn't seem to exist anymore.
The error: AttributeError: 'NoneType' object has no attribute 'click'
It is a button so shouldn't it be able to click?
How can I click the like/favorite button?
Error message:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted:
Element <div aria-label="394 Likes. Like" role="button" tabindex="0"
data-testid="like">...</div> is not clickable at point (539, 10).
Other element would receive the click: <div class="css-1dbjc4n r-1habvwh">...</div>
Solution:
browser = webdriver.Chrome()
infoq_url = 'https://twitter.com/infoqchina'
browser.get(infoq_url)
sleep(2)
like_buttons = browser.find_elements(By.XPATH, '//div[#data-testid="like"]')
like_btn = like_buttons[0]
like_svg = like_btn.find_element(By.TAG_NAME, "svg")
like_svg.click()
Reference:
btn_parent = btn.find_element(By.XPATH, "..") # find parent element.

Categories