So i have a log-in script and i just try find element by class on newest version of selenium (4.8.0)
code:
button = browser.find_element(By.CLASS_NAME, 'just class name')
AND this code not working.
my stuff:
firefox driver - 0.32.1 (2023-02-02, b7f075124503)
selenium 4.8.0
this is how the documentation searches for elements - By.CLASS_NAME, By.ID, etc...
https://www.selenium.dev/documentation/webdriver/elements/finders/
tried other variations of the code like find_element_by_ID and etc, installed an additional library for the selenium (I do not remember the name).
By.CLASS_NAME accepts only single classname as an argument. So you can't pass multiple classnames.
Solution
You can use either of the following locator strategies:
Using the classname just:
button = browser.find_element(By.CLASS_NAME, 'just')
Using the classname class:
button = browser.find_element(By.CLASS_NAME, 'class')
Using the classname name:
button = browser.find_element(By.CLASS_NAME, 'name')
As an alternative, you can also use a css_selector as follows:
button = browser.find_element(By.CSS_SELECTOR, ".just.class.name")
CSS classes and IDs never contain spaces. You can check if this class exists in the DOM by typing in browser console:
document.querySelectorAll('.class_name')
change class_name with class name you looking for. If the element has multiple classes each is prefixed by a period (.).
document.querySelectorAll('.class_name1.class_name2')
Related
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")
For clicking a button with the following HTML code:
All Notes
I have tried multiple ways to click/ access this button but to no success, for example:
all_notes = wait.until(EC.presence_of_element_located((By.XPATH, "//a[#href='javascript:submitPage('V','All Notes','');']"))).click()
all_notes = wait.until(EC.presence_of_element_located((By.XPATH, "//href[#type='class' and #value='View All']"))).click()
I am not really sure what the issue is here - for all other button presses on this webpage - selenium has been working appropriately. What other ways can I try?
all_notes = WebDriverWait(driver,10).until(EC.presence_of_element_located(
(By.XPATH, '//*[#href="javascript:submitPage(\'V\',\'All Notes\',\'\');"]'))).click()
escape the single quotes
Try this Xpath...
//a[#class='viewFilter' and contains(text(), 'All Notes')]
What this does is verifies that the class is viewFilter and since classes can appear more than once also ensure that the link text is All Notes.
<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
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()
textbox = driver.find_element_by_id('applicant.name')
doesn't work.
Unfortunately and I've tried every variation of inspect element, copy xpath , trying to identify the class name of the text boxes etc.
I want to be able to send some text to the input fields in the indeed-apply-widget seen here.
If you click 'apply now' (unfortunately I can't link it) you'll see the inputs are contained within 2 iframes.
Is there an easy way to access them?
Here's what I have so far:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://ca.indeed.com/cmp/Clearview-Plumbing-&-Heating/jobs/HVAC-Installer-Opening-6183dce44a9bfe10?sjdu=vQIlM60yK_PwYat7ToXhk42rccfWGklJDtD_zDpWBzDCfUEiSP2Zk-zLpFc6GNuF8wyV4_UaMyNFtpjETvX0fCpQXc3PxTbrGkwAMNkR5vGMtAfe9wNpTncItNgAIJHx' )
driver.find_element_by_class_name("indeed-apply-widget").click()
# move into the iframe
iframe = driver.find_elements_by_tag_name('iframe')[0]
driver.switch_to_frame(iframe)
Updated Code (still unable to interact with the iframe)
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://ca.indeed.com/cmp/Clearview-Plumbing-&-Heating/jobs/HVAC-Installer-Opening-6183dce44a9bfe10?sjdu=vQIlM60yK_PwYat7ToXhk42rccfWGklJDtD_zDpWBzDCfUEiSP2Zk-zLpFc6GNuF8wyV4_UaMyNFtpjETvX0fCpQXc3PxTbrGkwAMNkR5vGMtAfe9wNpTncItNgAIJHx' )
driver.find_element_by_class_name("indeed-apply-widget").click()
# move into the iframe
path = driver.find_element_by_xpath("/html/body/div[5]/div/div[2]/iframe")
driver.switch_to_frame(path)
driver.find_element_by_xpath('//*[#id="applicant.name"]').sendKeys("TEST_NAME")
The element that you want is inside two nested IFRAMEs. The below code should work. You need to go into the first frame... and then the first nested frame. The rest should work at that point.
driver = webdriver.Firefox()
driver.get('http://ca.indeed.com/cmp/Clearview-Plumbing-&-Heating/jobs/HVAC-Installer-Opening-6183dce44a9bfe10?sjdu=vQIlM60yK_PwYat7ToXhk42rccfWGklJDtD_zDpWBzDCfUEiSP2Zk-zLpFc6GNuF8wyV4_UaMyNFtpjETvX0fCpQXc3PxTbrGkwAMNkR5vGMtAfe9wNpTncItNgAIJHx')
driver.find_element_by_css_selector("span.indeed-apply-button-label")).click()
driver.switch_to_frame(0)
driver.switch_to_frame(0)
driver.find_element_by_id("applicant.name")).send_keys(username)
driver.find_element_by_id("applicant.email")).send_keys(email)
driver.find_element_by_id("resume")).send_keys(resumePath)
driver.find_element_by_css_selector("a.button_content.form-page-next")).click()
driver.find_element_by_id("apply")).click()