Instagram Following Selenium Python - python

I am trying to figure out how to get Selenium to click the following section of an Instagram account, here is the html for it.
<a class=" _81NM2" href="/peacocktv/following/" tabindex="0"><span class="g47SY lOXF2">219</span> following</a>
When using instagram, the direct /username/following link does not actually bring up the following list, you would have to click the field manually on the website for it to appear.
log_in = WebDriverWait(driver, 8).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type = 'submit']"))).click()
not_now = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]')))
not_now.click()
'''not_now2 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not Now")]')))
not_now2.click()'''
search_select = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#placeholder = 'Search']")))
search_select.clear()
'''search_select.send_keys("russwest44", Keys.ENTER)
search_select.send_keys(Keys.ENTER)'''
# Clicks who the person is following:
following = driver.find_elements_by_xpath("//button[contains(text(),'following')]")
following.click()
Which is the best way to come up with the solution

Seems like couple of issues with your current code:
# Clicks who the person is following:
following = driver.find_elements_by_xpath("//button[contains(text(),'following')]")
following.click()
if you pay attention, you've shared HTML with a tag, so your xpath should be
//a[contains(text(),'following')]
not
//button[contains(text(),'following')]
also, you are using find_elements_by_xpath it would return a list of web elements. you can not directly trigger .click on it.
either switch to find_element or
following[0].click()

Related

Having problem with the usage of driver find element by (Selenium)

I'am trying to make a bot that auto logins to a website.
In order to write the username I'm trying to use
driver.find_element_by_variable("username").send_keys(username)
When I'm looking that spesific variable from website on inspect the varible is two word like matinput formcontrolname.
On any other website if that varible is one word like id I simply write id after by_ and it works what can I do in this situation?
Selenium won't allow you to use:
find_element_by_variable()
but you have to use either of the predefined Locator Strategies as listed in the By implementation which are as follows:
CLASS_NAME= class name
driver.find_element(By.CLASS_NAME, "element_classname")
CSS_SELECTOR= css selector
driver.find_element(By.CSS_SELECTOR, "element_css_selector")
ID= id
driver.find_element(By.ID, "element_id")
LINK_TEXT= link text
driver.find_element(By.LINK_TEXT, "element_link_text")
NAME= name
driver.find_element(By.NAME, "element_name")
PARTIAL_LINK_TEXT= partial link text
driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
TAG_NAME= tag name
driver.find_element(By.TAG_NAME, "element_tag_name")
XPATH= xpath
driver.find_element(By.XPATH, "element_xpath")

Cannot click an element , Selenium Python

with selenium, i'm trying to click an element but not working with this element,
the page is here page (username/password :admin/admin)
wait2 = WebDriverWait(driver, 10)
element = wait2.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="operate2a5a0448a8bf44a8898ec13e95b152fc"]/div/div[2]')))
element.click()
i tried this on other element in the same page and got no problem
no idea why not working on this element
operate2a5a0448a8bf44a8898ec13e95b152fc seems to be dynamically created id.
The simplest way to access this element is with text based XPath locator:
wait2.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(),'Entry Registration')]"))).click()
you can try with below code as well :
//img[contains(#src,'registration')]/..
in code :
wait2 = WebDriverWait(driver, 10)
element = wait2.until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'registration')]/..")))
element.click()

How to get div text by id with selenium

I am trying to get a book's description text from its amazon webpage. I get the book's image and title and price fine by using driver.find_element_by_id, but when it comes to the description which is in a div with id="iframeContent", it doesn't work. Why? I have also tried WebDriverWait but no luck.
I use the following code:
def get_product_description(self, url):
"""Returns the product description of the Amazon URL."""
self.driver.get(url)
try:
product_desc = self.driver.find_element_by_id("iframeContent")
except:
pass
if product_desc is None:
product_desc = "Not available"
return product_desc
Since that element is inside the iframe you have to switch to that iframe in order to access elements inside the iframe.
So first you have to locate the iframe
iframe = driver.find_element_by_id("bookDesc_iframe")
then switch to it with
driver.switch_to.frame(iframe)
Now you can access the element located by the div#iframeContent css_selector
And finally you should get out from the iframe to the default content with
driver.switch_to.default_content()
Credits to the author
you need to use text method from selenium.
try:
product_desc = self.driver.find_element_by_id("iframeContent").text
print(product_desc)
except:
pass
Update 1:
There's a iframe involved so you need to change the focus of webdriver to iframe first
iframe = self.driver.find_element_by_id("bookDesc_iframe")
self.driver.switch_to.frame(iframe)
product_desc = self.driver.find_element_by_id("iframeContent").text
print(product_desc)
Now you can access every element which is inside bookDesc_iframe iframe, but the elements which are outside this frame you need to set the webdriver focus to default first, something like this below :
self.driver.switch_to.default_content()

Python Selenium Webdriver Message: Unable to locate element:

I'm trying to write a Python script using selenium's webdriver, to automate the task of uploading invoices to the federal online ledger.
I know this is a common question, but after reading many questions in SO and trying their answers, I couldn't find a solution.
This is the html code I want to select and click on:
<input id="idcontribuyente" type="hidden" name="idContribuyente">
<input class="btn_empresa ui-button ui-widget ui-state-default ui-corner-all" type="button" style= "width:100%" onclick="document.getElementById('idcontribuyente').value='0';document.seleccionaEmpresaForm.submit();" role="button">
These are the main things I've tried so far:
(first with the WebDriverWait() and then with time.sleep(20), just in case there was an error there)
#1
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "btn_empresa")))
empresa_btn = driver.find_element_by_class_name('btn_empresa')
empresa_btn.click()
#2
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn_empresa.ui-button.ui-widget.ui-state-default.ui-corner-all')))
empresa_btn = driver.find_element_by_css_selector('.btn_empresa.ui-button.ui-widget.ui-state-default.ui-corner-all')
empresa_btn.click()
#3
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "//input[#type='button']")))
empresa_btn = driver.find_element_by_css_selector("//input[#type='button']")
empresa_btn.click()
#4
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//input[#type='button']")))
empresa_btn = driver.find_element_by_xpath("//input[#type='button']")
empresa_btn.click()
#5
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//td[#align="center"]/input')))
empresa_btn = driver.find_element_by_css_selector('//td[#align="center"]/input')
empresa_btn.click()
#6
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all']")))
empresa_btn = driver.find_element_by_xpath("//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all']")
empresa_btn.click()
The error when using time.sleep(20):
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: element_name
The error when using WebDriverWait():
selenium.common.exceptions.TimeoutException: Message:
Here's the full html code:
I'm pretty new to this. What am I doing wrong?
many thanks in advance!
UPDATE - SOLUTION:
I found a way, I've used a Selenium IDE and exported the code to inspect it. Apparently it had to do with switching windows. This code works (although it may be more verbose than it needs to be):
vars = {}
vars["window_handles"] = driver.window_handles
# This line already worked before. It is for the previous page
driver.find_element_by_xpath("//div[#title='rcel']").click()
# Here it comes the way to select and click on the problematic button
def wait_for_window(timeout = 2):
time.sleep(round(timeout / 1000))
wh_now = driver.window_handles
wh_then = vars["window_handles"]
if len(wh_now) > len(wh_then):
return set(wh_now).difference(set(wh_then)).pop()
vars["win806"] = wait_for_window(2000)
driver.switch_to.window(vars["win806"])
driver.find_element(By.CSS_SELECTOR, ".btn_empresa").click()
This is how the actual webpage looked like:
red triangle indicates the button I wanted to click on
Maybe try...
driver.find_element_by_xpath("//input[#id='idcontribuyente']/following-sibling::input")
Since it is 2 input tags next to each other, this finds the one by ID first and takes the input sibling directly after it. Not sure it will work since I can't access the URL, but give it a shot
First wait, then click:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn_empresa.ui-button.ui-widget.ui-state-default.ui-corner-all')))
empresa_btn = driver.find_element_by_css_selector('.btn_empresa.ui-button.ui-widget.ui-state-default.ui-corner-all').click()
Some of the steps you've tried were very close.
If the element you want to click is hidden, try using driver.execute_script:
element = driver.find_element_by_css_selector("#idcontribuyente")
driver.execute_script("$(arguments[0]).click();", element)
Update 1, try to click the second input:
element = driver.find_element_by_css_selector(".btn_empresa.ui-button.ui-widget.ui-state-default.ui-corner-all")
driver.execute_script("$(arguments[0]).click();", element)
Update 2, probably you have few similar css locators similar to above:
try using:
tr:nth-of-type(4)>td>.btn_empresa.ui-button.ui-widget.ui-state-default.ui-corner-all
Update 3
Check if your button is located inside shadow DOM. If it is - none of the methods you tried will work. Check how to enable showing shadow DOM.
Update 4
As mentioned in the comment to your question, your second xpath was good.
Try using it this way:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all']")))
empresa_btn = driver.find_element_by_xpath("//input[#class='btn_empresa ui-button ui-widget ui-state-default ui-corner-all']").click()

How to click the icon using selenium

I am trying to automate my certain activities using selenium. I was launching a webpage and a security windows popup appears for the login credentials.
I have automated that using Autoit. Now after login I need to click on the option and I have tried it based on the find_element_by_text and find_element_by_id.
I was getting an error as Unable to find the element with CSS selector and I have seen some other post in the StackOverflow with the same issue but I could not able to fix it by myself.
Here is how my HTML looks like. Could you please guide me on this and also please share any document for further checking. Thanks.
driver = webdriver.Ie(r"C:\\IEDriverServer\\IEDriverServer.exe")
driver.get('URL of the page')
#driver.implicitly_wait(10) # seconds
autoit.win_wait("Windows Security")
# now make that crap active so we can do stuff to it
autoit.win_activate("Windows Security")
# type in the username
autoit.send('username')
# tab to the password field
autoit.send("{TAB}")
# type in the password
autoit.send('password')
# kill!
autoit.send("{ENTER}")
driver.maximize_window()
driver.implicitly_wait(120) # seconds
#submit_button_incidents = driver.find_element_by_link_text("3-Normal Incidents")
submit_button_incidents= driver.find_element_by_id("nodeImgs13pm")
submit_button_incidents.click()
driver.implicitly_wait(10)
++ updating the info
I have tried to copy the whole HTML but the page was restricted so I cant able to view the full HTML page other than the basic templates. Adding some more screenshots of the developer tools.
Here how my webpage looks like.
try with this code :
submit_button_incidents = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'My Group'])")))
submit_button_incidents.click()
and do not use implicit wait too many times in code.
Implicit wait is set for life time of web driver instance.
Reference :
Selenium official document for wiats
Xpath tutorial
cssSelector tutorial
UPDATE :
As OP has shared the HTML code. As per the requirement you can go ahead with this code.
As there are two elements with text as My Groups's queue.
For first one you can write XPATH as :
//a[#class='scbdtext']/span[contains(text(),'My Group')]
Code:
submit_button_incidents = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='scbdtext']/span[contains(text(),'My Group')]")))
submit_button_incidents.click()
For second one you can write XPATH as :
//a[#class='scbdtextfoc']/span[contains(text(),'My Group')]
Code :
submit_button_incidents = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[#class='scbdtextfoc']/span[contains(text(),'My Group')]")))
submit_button_incidents.click()
Hope this will help.
Use ActionChains with double click for this to work in Python.
from selenium.webdriver import ActionChains
# Get the element however you want
element = driver.find_element_by_xpath("//a[#class='scbdtextfoc']/span[contains(text(),'My Group')]")
ActionChains(driver).double_click(settings_icon).perform()

Categories

Resources