How to click a button on Facebook using Selenium - python

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

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

With selenium on python don't know how to shut down a banner which is preventing selenium from accessing the page content

I'm trying to open a site with Selenium (with Python) using Chrome browser, but when I do, a full screen promo banner immediately pops-up and I can't access the site content unless I close it.
On the top right there is an "x" as if it was a quit button, but actually it's an element ::before
and from its description it seems to me that it doesn't contain any button element.
If I operate manually, both clicking on the x and on the upper part of the page outside the banner, the latter closes, but I really don't understand how to access it with selenium.
The webpage I'm trying to open is https://sports.bwin.it/it/sports
Needless to say I'm quite inexperienced, so I hope this question won't sound too basic, but I wasn't able to find a solutione in the selenium docs or on the web; if someone could give me any hint I would appreciate it.
This is a screenshot from the page I'm talking about
This is part of the html code from the web page; the element I am talking about is the one pointed by the arrow;
Based on your screen shot the xpath you want to use would be something like this:
//*[#data-id='dj_sports_c_ovl_br']//span
full code would be something like this:
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//*[#data-id='dj_sports_c_ovl_br']//span"))
)
element.click();

python selenium youtube like button

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")]

Selenium PhantomJS Scrolling Down

I tried to scrolling down with selenium, But i use webdriver PhantomJS. I tried selenium for test javascript instagram. As you know, on instagram there have button "Load More", after click button "Load More", we don't have to click again becouse it will auto refresh and it will show more image.
I tried like this :
driver.find_element_by_xpath("//a[#class='_8ioip _glw1t']").click()
time.sleep(5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3.6)
Actually this code it's work, but not really work, i mean something weird happening at the "time.sleep". If i give 2, the result are few and if i give 3 the result are just pretty much, But if i give 5, the result same like i give 2.
The question is, How to make teh Scroll get all data
class of "Load more" button seems to have changed.
your code is correct, try with this xpath:
//a[#class='_8imhp _glz1g']
or try to locate the button by it's text content:
//*/div/a[text()[contains(.,'Load more')]]
and before click the button scroll down.. so your code should looks like:
driver.get('https://www.instagram.com/explore/tags/whatever/')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.find_element_by_xpath("//*[text()[contains(.,'Load more')]]").click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

Categories