I am trying to find the textbox element using the find_element_by_xpath() method, but It keeps telling me it cant find said element, here's the line of code that does that.
I've tried finding it by link_text, partial link text, selector and it just doesn't work
bar = nav.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div/aside/div[2]/div[2]/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
Thanks in advance!
So, I suggest creating your xpath if you want to be precise and avoid taking it based on html structure (which can change).
The locators looks like:
And you can take it with xpath:
//input[#placeholder='Search people' and #role='combobox']
To avoid this problem, I suggest going trough a tutorial for a better understanding regarding how to create custom locators: Xpath tutorial
What was happening is: When I opened the tab to inspect the element, the DM structure changed because of my screen size so the xpath wasn't the same
Related
How can I find the log in button element? My script as below but not working after input username & password. Or shall I use find element by css selector or other? and how?
driver.find_element_by_name("username").send_keys(username)
driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_class_name("cux-button ng-scope").click()
enter image description here
I assume that you are using Selenium and the Selenium Python bindings. (Tipp: Mention the library, you are using, in your question. :) )
The find_element_by_class_name() method only allows searching for a single class name, as already discussed in this question. As mentioned in the answers to that question, you can use the find_element_by_xpath() or find_element_by_css_selector() methods for more complex queries.
In your scenario, CSS selectors are sufficiently powerfull, so let's use them:
driver.find_element_by_css_selector(".cux-button.ng-scope").click()
For understanding CSS selectors, you might want to take a look into this tutorial by Sauce Labs, as recommended by the unofficial Python Selenium docs.
I am using Selenium with Python to automaticlly extract some data from our power plants and right now I need to click on an element.
The problem is that the element's xpaths and order change for each plant we are monitoring. The only static info is the value, just like in the 3rd line value="T_U0.
I tried many approaches and I couldn't find a solution. I can't use index or child because the order of the parameters is changing. I tried CSS selector with no success.
Here you get some of my tries...
driver.find_element_by_xpath("//input[#value='T_U0']").click()
driver.find_element_by_css_selector("input[#data-id-sys-abbreviation='388']").click()
I tried many other things but I was just desperately trying anything.
What I really need is a find_by_value, if there is a way of doing it please let me know, if there isn't please show me how I can do it.
I need to click in some options that change order accordingly to the plant
The problem is with the first xpath. You are trying to locate an input while you need to get option.
Try this:
driver.find_element_by_xpath("//option[#value='T_U0']").click()
You can try to click/select element via displayed text.
Pseudo code:
driver.find_element_by_xpath("//option[text()="Some text"]").click()
So i was just trying to see if i could automate myself logging into the nike website when i realised that the xpath they use for the input changes every time you reload the page.
The only way i managed to find the element was by searching for it by the tag name input . However, when i then try to send the keys to it it gives me the error saying "element not visible". My question is how would i send the keys to it or is there a way of finding the xpath of an element each time i run the code by finding the element by the tag name (input)
Here is a copy of my code:
driver.get("https://www.nike.com/gb/en_gb/p/activity/login")
time.sleep(3)
driver.find_element_by_tag_name("input").send_keys("test")
And the website of the form is :https://www.nike.com/gb/en_gb/p/activity/login
Thanks in advance guys :)
I am able to set the email and password with below code
driver.find_element_by_xpath("//input[#type='email']").send_keys("abcd")
driver.find_element_by_xpath("//input[#type='password']").send_keys("abcd")
You can also use cssSelector to find elements:
driver.findElement(By.cssSelector("input[name='emailAddress']")).send_keys("abcd");
driver.findElement(By.cssSelector("input[name='password']")).send_keys("abcd");
Using css-selector is faster than xpath and, in my opinion, it's also easier to declare and customize.
I am pretty new to selenium and I am trying to figure out how to simulate a onclick
this is what I see in the source code when I inspect the html source
<img src="images/ListingOptionSearch.jpg" onmouseover="this.src='images/ListingOptionSearchHover.jpg'" onmouseout="this.src='images/ListingOptionSearch.jpg'">
I tried :
driver.find_element_by_css_selector("a[onlick*=document.getElementById('pN')
.selectedIndex]").click()
but I get a InvalidSelectorException
any idea?
thanks!
You can use
from selenium import webdriver
browser = webdriver.Chrome(somepath) # You should know what this does.
browser.execute_script("document.getElementById('pN').selectedIndex = 0;document.getElementById('optionList').submit();return false")
Meaning you can execute Javascript code just by using .execute_script , Awesome, right?
An invalid InvalidSelectorException is an expecting raised when there's no element or from experience, there could be an iframe and you'll have to use .switch_to.frame to be able to interact with it.
Also, I like using XPath (most reliable always), it takes a little bit time getting used to, but with an hour or two of practising you can get it.
JeffC has a good point, the structure of the HTML, JS can always change.
You can use the find_element_by_xpath(xpath).click() but there are also more dynamic ways to predict wether the structure is going to change, using something like find_element_by_nameor other that are available:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
Did you google the exception? It means that your selector is not valid. CSS selectors like the one you are trying to use are in the form tag[attribute='value']. You don't have the value surrounded by quotes... which is not going to be possible in this specific case because your value already contains single quotes.
Because the A tag encloses the IMG tag, you can click on the IMG tag and get the same effect. A CSS selector like the below should work.
img[src='images/ListingOptionSearch.jpg']
There are other selectors that would probably work but with a link to the page, etc. I would be just guessing as to whether they would be unique.
im new to selenium and I am trying to click on an image but when I inspect the element it only has a title, an src=, and an alt=. I would like to use xpath but am unsure exactly how it works on python, on java i would do driver.findElement(By.xpath("//img[#title='']")) but I am unsure of how to do it on python any help is appreciated :)
In Python, there is the find_element_by_xpath() shortcut method:
driver.find_element_by_xpath("//img[#title='']")
Note that your locator does not look quite reliable. Providing the relevant HTML of the image and its parents might help us to provide you with a better option.