Hopefully someone can help me out here.
My goal is to successfully connect to this website using Python (Selenium + Chromedriver) and successfully search for an address in the search bar. Im struggling to manage to find the actual search bar itself using Selenium. The HTML code doesn't have an ID or NAME to distinguish it by, all it has is a class and when i search for the class, i get an error saying it cannot be found.
Hopefully this is an easy fix and im just being silly. Thanks a lot.
P.S First post - please drop some hints on what to change next time :)
CODE
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://creeper.banano.cc/")
search = driver.find_element_by_class_name("ValidatedSearch form-control form-control-lg ")
ERROR CODE
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".ValidatedSearch form-control form-control-lg "}
Python Code + HTML of Website
ValidatedSearch form-control form-control-lg
is what are referred to as multiple class names.
In order to find the element we use css selector.
driver.find_element(By.CSS_SELECTOR,".ValidatedSearch.form-control.form-control-lg")
Also don't use driver.find_element_by since they are depreciated instead import By and use that.
The error here is you have multiple class names in a function only expecting one. ValidatedSearch, form-control, and form-control-lg are different classes, since classes can't have spaces in them. So what you're looking for is this:
search = driver.find_element_by_class_name("ValidatedSearch")
This targets the first tag in the ValidatedSearch class that it finds.
I was able to get it to work with
search = driver.find_element_by_class_name("ValidatedSearch")
search.send_keys("Hi My Name is Slicka Slicka Slim Shady")
I used one class. I would say Selenium iirc only requires a single class. Also Im not primarily a webdesigner but these frameworks that are all over today are pretty much add the additionals are annoying. When I create Selenium scripts I use xpaths more and more more. You may want to look into that
Related
Python newbie here. All of 1 month old. Most of the elements in page i am trying to scrape, i am able to handle them and interact with them. there are two elements that have dynamic labels which i am not able to handle. The source page looks as below
<span class="a-button-inner">
<input class="a-button-input" type="submit" aria-labelledby="Ae8MCi-55">
<span id="Ae8MCi-55" class="a-button-text" aria-hidden="true">Add Your Key</span>
</span>
Upon each refresh the label is new so i cannot get a fixed XPATH of that. There are multiple items with span class "a-button-inner" as well as input class "a-button-input" and there are other submit buttons as well in the rest of the page. The only thing unique is the Span text "Add Your Key".
Appreciate all help to get the submit button element and to click/submit it.
from selenium import webdriver
.
.
.
.
# objAddKey = driver.find_element_by_xpath('//*[#id="Ae8MCi-55"]/span/input') does not work as second round its a different XPath
objAddKey = driver.find_element_by_link_text('Add Your Key') # hoping this will get a sibling and then to get the parent and then look for all the children. I don't even know if its possible in python.
objAddKey.click()'
Tried to search for searching by span text, and came across some other stuff,
https://www.tutorialspoint.com/how-to-get-text-found-between-span-selenium
WebElement l = driver.findElement(By.xpath("//p/span"));
String s = l.getText();
but this does not help either.
https://selenium-python.readthedocs.io/locating-elements.html
went through this, but not much help either.
Appreciate your help and pointers.
Thank you.
If Add Your Key is unique then you can use the below xpath :
//span[contains(text(), 'Add Your Key')]//preceding-sibling::input[#class='a-button-input']
or even without class :
//span[contains(text(), 'Add Your Key')]//preceding-sibling::input
and use it like this :
objAddKey = driver.find_element_by_xpath("//span[contains(text(), 'Add Your Key')]//preceding-sibling::input")
objAddKey.click()
I would suggest you to have explicit waits for more reliability.
Selenium - Python - Explicit waits
So basically I have something like this...
<div class="123">ex1</div>
<div class="456">ex2</div>
<div class="789">ex3</div>
<div class="100">ex4</div>
<div class="123 456 789 100">ex5</div>
<a aria-label="Title" class="the same problem"></a>
And I want to select the 5th div, without selecting other 4.
I've seen some people doing this:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
driver.findElement(By.cssSelector("div.123.456.789.100"));
But when I tried to install the org module I got "Could not find a version that satisfies the requirement org" error.
So I found this:
driver.find_elements_by_xpath("//*[#class='123' or #class='456' or #class='789' or #class='100']")
But I also got
"selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//*[#class='123' or #class='456' or
#class='789' or #class='100']"}"
error.
I've done my research, nothing helped so I decided to ask here.
Also I want to get the aria-label text from that "a" object later on, it has the same problem I described before. I'll probably figure it out myself tho. For now I really want to know how to make it work.
First of all, what language do you use?
by import org. it's java if i'm not mistaken, then you're using python.
to find element in python, just:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://you_site')
driver.find_element_by_xpath('//div[#class="123 456 789 100"]')
try this,
I know the basics of python and I am trying out Selenium. This page I am on is my schoolwork website. I want to select every single element in the timeline of my homework. I tried to select the whole timeline with XPath or even just one specific homework(elem) but both have this error code:
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="jwdd3b016b_acnt"]/div[3]/ul/li[3]"}
(Session info: chrome=81.0.4044.122)
This is the code:
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\tomas\\Downloads\\chromedriver_win32\\chromedriver')
driver.get('https://glnt.edupage.org/timeline/')
#then I log in but that's not important
important_stuff = driver.find_element_by_xpath('//*[#id="jwdd3b016b_acnt"]/div[3]/ul/li[3]')
I have no idea what to do. I was under the impression that when u use XPath the code should always work. Am I wrong? (not always I mean, but when the code is correct)
Edit: sorry I forgot here is the HTML of the element:
<li class="hwItem m04 d5" data-typ="timeline" data-homeworkid="timeline:2031705" data-date="2020-04-24" data-timelineid="2031705">
That should do it:
<div class="edubarMainNoSkin">
<div id="jwdd3b016b_acnt" class="hwMainListMain">
<div class="hwListElem">
<ul class="hwList timelineMode ">
I couldn't say much without seen more of the HTML.
//*[#id="jwdd3b016b_acnt"]/div[3]/ul/li[3]
id part may not be constant.
It might be inside of an iframe, so you have to switch into it.
Due to browser rendering, deepness of the div might be changed.(div[3] would be changed)
First find the correct element using developer tools.
Then while executing using webdriver put a break point and check via developer tools.
<input class="select_step ON " id="step_9285985_select" name="step_9285985_select" type="checkbox" value="9285985">
I am automating the regular tasks of our project by python selenium. struct at one step, Please help me.
As per the above html code id="step_9285985_select" randomly changing for every time browser opens through webdriver (python Selenium). I want to find the randomly generated "id" by the class="select_step ON or any other alternative.
First find the element by CSS selector:
input_tag = driver.find_element_by_css_selector('input.select_step.ON')
Then get the id attribute:
print(input_tag.get_attribute('id'))
I'm trying to click the following link using selenium.
<div id="RECORD_2" class="search-results-item">
<a hasautosubmit="true" oncontextmenu="javascript:return IsAllowedRightClick(this);" class="smallV110" href="#;cacheurlFromRightClick=no"></a>
</div>
Which record to click is not known before the code is executed. Record_2 has multiple children, and the one included is the one I want to click. The link is edited for the sake of privacy. I tried to do something like that where name is the record variable, however it doesn't work.
driver.find_element_by_css_selector("css=div#"RECORD_%s" % (name).smallV110")
I'm a complete newbie to selenium so I couldn't figure out a way to sort this out. I would appreciate any help. Thanks!
Note that this is not Selenium IDE and you don't need the css= at the beginning of a selector.
There are multiple ways to locate the link element, e.g.:
driver.find_element_by_css_selector(".search-results-item a.smallV110")
driver.find_element_by_css_selector("[id^=RECORD] a.smallV110") # id starts with "RECORD"
If you know the id value beforehand:
id_i_know = 2
driver.find_element_by_css_selector("[id=RECORD_%d] a.smallV110" % id_i_know)
You don't have to have that smallV110 class attribute check - I've added it to increase chances of not matching other a elements inside the div (not sure what they are, you have not posted the entire HTML).