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

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

Related

Python Selenium - Constantly changing web element ID

I'm trying to use Selenium to find a web element that I know is the To field in a web email application (please see picture). I am able to successfully identify this web element and use send_keys to send an email address to this field.
However, the issue is the id always seems to cycle between 299 and 3 other numbers like 359 or 369.
Here's the code Im using. Is there another way I can account for this changing ID?
to_field = wait.until(EC.element_to_be_clickable((By.ID, "v299-to-input")))
print(to_field)
to_field.send_keys(email_reciever)
Thanks
PS- The web email application is fastmail.com
Since the ID attribute is changing, you have to build your locator on some other, stable parameter. The following CSS Selector is stable:
to_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".s-compose-to textarea")))
print(to_field)
to_field.send_keys(email_reciever)
The same can be done with the use of XPath locator:
to_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'s-compose-to')]//textarea")))
print(to_field)
to_field.send_keys(email_reciever)
You can use following css selector to identify the element.
to_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea[id$='-to-input']")))
print(to_field)
to_field.send_keys(email_reciever)
This will identify the element id ends with the -to-input static value
to_field = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.v-EmailInput-input")))
print(to_field)
to_field.send_keys(email_reciever)
This will identify the element with tagname.classname

Instagram Following Selenium 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()

How to find an element (button) without the ID, Value and Type using selenium?

<button class="button" style="styles">TEXT</button>
How do I find this element in a website using Selenium PhantomJS?
You can use the XPATH, ro the element you want to find by using:
from selenium.webdriver.common.by import By
myButton = driver.find_element(By.XPATH, '//button[text()="Some text"]')
But using the XPATH is not the best practice sinze it can change if the html changes.
Other way to lacate it is using the tag "button":
myButton = driver.find_element_by_tag_name('button')
or the class:
myButton = driver.find_element_by_class_name('button')
There are several ways to find it, you can check them here:
enter link description here

How to get iframe source from page_source when the id isn't on the iframe

Hello today i wanna ask how to get the link inside the page source but without id, i asked before how to get the link with id ok now i understand, but i've tried the same method with another link and i was not successful about that so here is my code:
from selenium import webdriver
# Create a new instance of the Firefox driver
driver_path = r"C:\Users\666\Desktop\New folder (8)\chromedriver.exe"
driver = webdriver.Chrome(driver_path)
# go to the google home page
driver.get("https://www.gledalica.com/sa-prevodom/iron-fist-s02e01-video_02cb355f8.html")
# find the element that's name attribute is q (the google search box)
element = driver.find_element_by_id("Playerholder")
frame = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame("iframe")
link = frame.get_attribute("src")
driver.quit()
Like this here: enter image description here
There are multiple way to get it. In this case one of easiest is by using a CSS selector:
frame = find_element_by_css_selector('#Playerholder iframe')
This looks for the element with id = "Playerholder" in the html and then look for a child of it that is an iframe.

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