python selenium youtube like button - python

So I am trying for over an hour now to make selenium click the youtube like button...
Nothing works on google and I have no idea what to do anymore.
If someone can help me that would be amazing
(Im kinda new to python)
Thanks!

So following on my comment, you need to inspect the xpath of the youtube like button.
Right click on the like button, press inspect element. It should show a console and in the console the like button path should be highlight. Click on it then click copy xpath.
After that do what you need to do, e.g like_button_click = driver.find_element_by_xpath('xpath of the like button').click()

Selenium docs: https://www.seleniumhq.org/docs/
The below is tested and working - remember that to like videos, you always need to be logged into your Youtube account.
1) Identify the buttons xPath:
To extract the specific Like button xPath, you can inspect the button through any developer browser tool, right click on it and select Copy xPath:
Then just copy the xPath into here:
button = driver.find_element_by_xpath("here")
2) Log into Youtube and click on the Like button
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Chrome()
youtube = driver.get("https://www.youtube.com/watch?v=9fHt-VVG_hg")
sleep(5)
button = driver.find_element_by_xpath("//*[#id='top-level-buttons']/ytd-toggle-button-renderer[1]/a")
ActionChains(driver).move_to_element(button).click(button).perform()

Try this one
//yt-animated-icon[contains(#animated-icon-type,"LIKE")]

Related

Python Selenium, edge browser, I dont see Inspect Element on every element

I am trying to get data from a Power Bi table. There are some elements that appear when hovering over a table. When I right click on ... I don't see Inspect Element. However, when I left click on this element, I can see a menu, and if I right click on any items, I can see Inspect element.
My first question, is why I don't see Inspect Element in the right click menu for all elements in the browser. Am I somehow able to open this ... menu programmatically in Selenium?
the Export Data element only appears in HTML after the first left click. I'm assuming this is created using Javascript and in order to export data with Selenium I would have to programmatically instantiate this by clicking on the ... menu. Is selenium capable of triggering javascript functions that generate more html code in a dynamic webpage? Or do I need to somehow click on the ... element.
If I can execute a javascript function, how can I find out in Edge the javascript function that gets executed and how can I replicate this function in Selenium
Essentially, if I try to find the Export data element in Selenium, it is not able to find it, unless I set a breakpoint before search, then in EdgeDriver I open this menu, and then I can find it and click it through Python
If all else fails, can I programmatically open the left click menu by automating a mouse click at certain coordinates in Selenium?
1.1 why I don't see Inspect Element in the right click menu for all elements:
PowerBi has its own context menu so they suppress the browsers context menu. If the element is tricky to find the dev tools, you can press Ctrl + Shift + C (while dev tools is open) and then click the desired element. Your mouse needs to be already over the element before pressing the key combination.
1.2 Am I somehow able to open this ... menu programmatically in Selenium?
Seems a little tricky, but could work if you first find the title of that area and move the mouse there, like described here: https://stackoverflow.com/a/8261754/12914172
Then your element should be in the html and you can find it hopefully by its class name vcMenuBtn that seems to be unique on that page. But you need to verify that.
2. Is selenium capable of triggering javascript functions that generate more html code in a dynamic webpage? Or do I need to somehow click on the ... element.
Selenium is able to execute javascript like desribed here: https://stackoverflow.com/a/70544802/12914172
However in your sample, and I was quickly checking the PowerBI online page, this looks like a whole lot of reverse engineering to understand and can sometimes be dangerous as well. I would go for hoover over the area find the ... and click it.
3. How can I find out in Edge the javascript function that gets executed
In dev tools you can set breakpoints to debug the steps the pages does after an action. But again, I would not invest to much time in that.
4. Can I programmatically open the left click menu by automating a mouse click at certain coordinates in Selenium?
Yes but this never works as good as the way described above. If you still want to give it a try, maybe that answer helps: https://stackoverflow.com/a/26385456/12914172
Many thanks to r000bin, this solution works for me, downloading data from PowerBI using Selenium for Python:
import selenium, mouse, time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
url = 'https://dataport.gasunie.nl/Vulling-Gasopslagen-Nederland'
driver = selenium.webdriver.Chrome(service=Service())
driver.get(url)
time.sleep(4)
#driver.fullscreen_window()
#driver.switch_to.window(driver.current_window_handle)
time.sleep(4)
iframe = driver.find_elements(By.TAG_NAME, 'iframe')
assert len(iframe)==1
driver.switch_to.frame(iframe[0])
time.sleep(4)
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element_with_offset(driver.find_element(By.TAG_NAME, 'html'), 0,0)
actions.move_by_offset('5', '5').click().perform()
time.sleep(4)
button = driver.find_element(By.CLASS_NAME, 'vcMenuBtn')
button.click()
button = driver.find_element(By.ID, '0')
button.click()
# 4 tabs and 1 enter
time.sleep(4)
for n in range(4):
element = driver.switch_to.active_element
time.sleep(2)
element.send_keys(Keys.TAB)
time.sleep(2)
element = driver.switch_to.active_element
time.sleep(2)
element.send_keys(Keys.ENTER)
driver.close()

I am having trouble clicking the signup button on a reddit sign up page with selenium using python

I am trying to submit a form to create a new reddit account using selenium at https://www.reddit.com/reddits/login/ . I am new to using selenium and web scraping in general and need some help selecting and ultimately clicking the sign up button on this webpage after all the details have been entered in. I have tried to select it using
submit = browser.find_element(By.CSS_SELECTOR("//*[#id="register form"]/div[7]/button")).text
along with trying to grab it through its xpath which is
//*[#id="register-form"]/div[7]/button
but am having no luck. The farthest I am able to get is having it say a 'str' object is not callable using Selenium through Python as an error message or it will say element not found. Any help would or guidance in the right direction would be greatly appreciated.
I am able to enter in all fields of data and beat the re-captcha that pops up also and be entirely good to go except for this sign up button. I am not familiar with this much so I am not sure why any of the responses answers worked when I tried them. I am not sure if it is something about the button or if its just how the page is setup or if its needing to run some javascript with the button click. I am not sure why this button cannot be clicked.
To solve the re-captcha I load another webpage and delete it to get back to this one not sure if this is relevant or not.
Picture of the Button I am trying to click on with selenium
Sign Up Button Picture
SOLVED: All of these solution below work for accessing the button. My mistake was switching to a different tab and not properly switching back. So I was trying to click an element on an old tab not the main sign in page. Once the focus was shifted back to that page the element was found and clicked!
The CSS Selector for that submit button would be:
"#register-form button"
You can verify from the console of a web browser by calling:
document.querySelector("#register-form button")
XPATH worked for me:
url = driver.find_element(By.XPATH , '//*[#id="register-form"]/div[7]/button')
url.click()
wait = WebDriverWait(driver, 30)
driver.get("https://www.reddit.com/reddits/login/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#user_reg'))).send_keys('user')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#passwd_reg'))).send_keys('pw')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#passwd2_reg'))).send_keys('pw')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#email_reg'))).send_keys('user#email.com')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#register-form > div.c-clearfix.c-submit-group > button'))).click()
The full signup for that would be like so you would want to click the element of button. Replace keys with values you need.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Scraping Instagram followers for an account

I'm trying to scrape followers from an Instagram account by using Selenium. (https://www.instagram.com/france/followers/)
I need to scroll down a popup page but the only thing I can scrolldown is the back page.
Here is my code
scr1 = driver.find_element_by_xpath('/html/body/div[4]/div/div')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);",scr1) ## Scroll to bottom of page with using driver
I also tried in my JS browser console by using the JSPath of my dialog modal :
window.scrollTo(0, document.querySelector("body > div.RnEpo.Yx5HN > div"));
(Already saw Stack posts related but answers seems deprecated in Oct. 2020)
I've had similar issues with what you are trying to do. Currently, the method I have implemented in my script uses Selenium's ActionChains class which I find is much more helpful than all the Javascript (execute_script) answers out there.
Basically I use ActionChains to press the "down" arrow key and then manipulate it to my advantage. I'll lay out the basic syntax below:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(browser)
actions.send_keys(Keys.DOWN).perform() #sends a general down arrow key to the page
Now I focus the ActionChains on a specific element, in this case it's the popup. There's a ton of ways to focus on the element but I find the easiest workaround is simply clicking the element before using ActionChains
Like this:
popup = driver.find_element_by_class_name('popupClass') #you don't have to use class_name
for i in range(500): #500 is amount of scrolls
popup.click() #focus the down key on the popup
actions.send_keys(Keys.DOWN).perform() # press the down key
The above method will click on the popup and then press the down key once, essentially sending 500 "scrolls" to the popup rather than the whole page. Once it's done, you're gonna wanna grab the .text property on the element responsible for the popups (at least that's what I did in my script).

How to click a button on Facebook using Selenium

I wanted to click a "see more" button on a Facebook page, but nothing seems to work.
The code is something like this:
from selenium import webdriver
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get(url)
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
browser.find_element_by_id("reaction_profile_pager1").click()
print("Click Successfull")
time.sleep(2)
I have tried literally every way one can find the thing to click in selenium (id, class, link name, name, etc.) and there is always a different error. However, I may have messed up with Xpath.
This is how the page code looks like:
Here is the link to that
The only thing that I want to do is press all "see more" buttons until no more is left.
I hope that someone knows what to do. Thank you for replies.
browser.find_element_by_xpath("//*[#class=\"clearfix mtm uiMorePager stat_elem _52jv\"]").click()

How to click this button with python & selenium

I'm looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.
I'm having trouble identifying the button to then call the click function.
Here's the HTML
Alternatively perhaps I could run the 'ng-click' function, unsure how to approach that. If I do this, I'll need to pipe through 'index', from what I can tell from this HTML (my understanding of HTML is minimal)
Thank you
You can have browser to figure out the button CSS selector for you.
Here's how to do that in Chrome:
Open your page in Chrome
Right-click on your button and select the Inspect Element from the context menu
The Inspector window will open with the button's HTML markup selected.
Right-click the selected HTML line(s) and select Copy -> CSS Selector.
Paste the CSS selector into the code below
And here's the code to click your button:
from selenium import webdriver
browser = webdriver.Chrome('/path/to/chromedriver')
browser.get('your/website/url')
button = browser.find_element_by_css_selector('paste the CSS selector here')
button.click()
Hope this helps.
PS: Here's an excellent article (a chapter from the Automate the boring stuff with Python book) on web scraping and browser automation using BeautifulSoap and Selenuim.
Try for xpath as:
//div[#id='channel']//div[#class='channel-list']/div/div/div[#class='ch-btn play']
or
//div[#id='channel']//div[#class='channel-list']//div[#class='ch-btn play']
Let me know if this Answers your Question.
You'll probably want to use CSS Selectors, as they are preferred in selenium over Xpath. Some important notes about html and selenium:
Html is a static language. There is not way to "call" or "run" things in it. That requires use of a different language, like JavaScript.
Selenium mimics an actual user, so selenium is not directly "calling" anything, it is interacting with the page, and the page responds.
python code:
driver = webdriver.Chrome('path/to/chromedriver')
driver.get('your/site/here')
# This is a css selector for the div that you want to click on.
css_selector = "div[ng-click='play($index)']"
# This finds the object that is located at css_selector
button_element = driver.find_element_by_css_selector(css_selector)
# Sends a mouse click to the button_element
button_element.click()

Categories