Selenium click like on twitter - python

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.

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

can't click the checkout button on shopee using selenium

Trying click this shopee checkout button. can't continue to the next process. Here's the html element
html :
<button class="shopee-button-solid shopee-button-solid--primary " xpath="1"><span class="pcmall-cart_1FvSs2">checkout</span></button>
There is a my code:
def click(browser,element_css):
WebDriverWait(browser,60).until(
ec.visibility_of_element_located((By.CSS_SELECTOR, element_css)))
WebDriverWait(browser,60).until(
ec.element_to_be_clickable((By.CSS_SELECTOR, element_css))
)
browser.find_element_by_css_selector(element_css).click()
click(browser,'.shopee-button-solid')
wait.until(
ec.presence_of_element_located((By.CSS_SELECTOR,'.loading-spinner-popup'))
)
Error Code python selenium:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button class="shopee-button-solid shopee-button-solid--primary ">...</button> is not clickable at point (984, 524). Other element would receive the click: <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 44 22" class="chat-icon">...</svg>
It doesn't work anymore and the button doesn't get clicked. Is there a way to solve this?
You could have used the explicitWait
check_out = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"")))
check_out.click()
So the code can be like
check_out = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//*[#id="main"]/div/div[2]/div[2]/div/div[3]/div[2]/div[7]/button[4]")))
check_out.click()

How to click css selector by text using selenium python

I would like to test some actions. In this case, I need to click on the text to get an upload form, but none of the forms below do not lead to the desired result.
This part of HTML-code, where I need to do some actions:
<a href="javascript:;" class="dg-hider st-mb__20 st_add-material__link" onclick="iu.ajax(event,'https://alexanderro.com/ajax/documentAddGsiUpload');">
Choose your category
</a>
<div id="GSIData"></div>
none of these works:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "dg-hider st-mb__20 st_add-material__link"))).click()
driver.find_element_by_class_name("dg-hider st-mb__20 st_add-material__link").click()
driver.find_element_by_id("GSIData").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "GSIData"))).click()
driver.find_element_by_css_selector("a[onclick*=https://alexanderro.com/ajax/documentAddGsiUpload]").click()
Could you help me?
You need to wait for the element for its presence or visibility:
You can use the below code snippet, it should work
def wait_for_element_to_be_clickable(element):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, element)))
element = "a.dg-hider.st-mb__20.st_add-material__link"
wait_for_element_to_be_clickable(element)
driver.find_element_by_css_selector(element).click()
print("Clicked")
I can't make a comment in Stack Overflow, so I'll try help you throug here:
Did you try:
driver.find_element_by_css_selector('[class="dg-hider st-mb__20 st_add-material__link"]').click()
or
driver.find_element_by_xpath('"//a[contains(text(), 'Choose your category')]"').click()

How to click on an element by partial_link?

this is ths html code :
<a href="https://www.decathlon.ca/fr/12547-cyclisme" xpath="1"> Cyclisme
</a>
And im trying to click on this element by this code but not working (Timeout) :
def click_cycling_menu(self):
cycling_menu_id = WebDriverWait(self.driver, 50)
cycling_menu_id.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "12547"))).click()
Can you use XPath contains functionality?
Feed this into find by xpath:
//a[contains(#href,"12547")]

python element is no longer attached to the dom selenium

I get "element is no longer attached to the dom" exception even though the element is there and is clickable, I am trying to click the "next" arrow on ryanair website, the html for the next button is:
<li class="newer" ng-class="{'loadingsmall':loading}">
<a ng-disabled="loading" ng-click="loadMore(0, 'SelectInput$LinkButtonNext1', 1)"
title="Next Week" href="">
</a>
</li>
I located and clicked it in several methods:
elem = WebDriverWait(browser, 15).until(EC.element_to_be_clickable((By.XPATH,
"//a[#title='Next Week']")))
elem = browser.find_element_by_xpath("//a[#title='Next Week']")
elem.click()
and:
area = browser.find_element_by_xpath("//a[#title='Next Week']")
action = webdriver.ActionChains(browser)
action.move_to_element(area) action.click(area) action.perform()
and:
elem = browser.find_element_by_link_text('>')
elem.click()
all work fine if I have no action in between, but once I tell selenium to click on other elements on the page (I do not move to other pages, I stay on the same page and show some dynamic content) the "next" link only works the first time around, and then gives me the exceptions, help would be so greatly appreciated! :)
You're still on the same page but the DOM's elements have changed due to some AJAX actions & calls, so you're forced to re-detect your object using:
elem = browser.find_element_by_link_text('>')
If you don't do that, it is more than likely that you'll get a
StaleReferenceException
For more info

Categories