Can't find element that exists using Python Selenium IE - python

I'm trying to type text in a text box
<input name="pesquisarUtente.numeroUtente" class="number numero-utente" id="pesquisarUtente.numeroUtente" type="text" maxlength="9" value="">
I've tried the following
driver.find_element_by_name("pesquisarUtente.numeroUtente").send_keys('123123123')
driver.find_element_by_id("pesquisarUtente.numeroUtente").send_keys('123123123')
I always get the error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == [id="pesquisarUtente.numeroUtente"]

Try using Xpaths or Css Selectors.
This error commonly occurs when there is more than one element with the same id so selenium is expecting find_elementS_by_id.
Xpaths and Selectors are unique to each element so they work better when trying to find a specific element.
example using xpaths:
driver.find_element_by_xpath("ENTER XPATH HERE").send_keys('123123123')
To get the Xpath of an element you simply 1: hover over the element in the developer mode 2:right click on it 3:hover over the copy option 4:move down to copy Xpath and click
Css Selectors work the same way. (driver.find_element_by_css_selector("SELECTOR HERE"))
And you can see the option to copy one 3 above the option to copy the Xpath

Related

How do I click on an item on my google search page with Selenium Python?

Good time of the day!
Faced with a seemingly simple problem,
But it’s been a while, and I’m asking for your help.
I work with Selenium on Python and I need to curse about
20 items on google search page by random request.
And I’ll give you an example of the elements below, and the bottom line is, once the elements are revealed,
Google generates new such elements
Problem:
Cannot click on the element. I will need to click on existing and then on new, generated elements in this block: click for open see blocks with element
Tried to click on xpath, having collected all the elements:
xpath = '//*[#id="qmCCY_adG4Sj3QP025p4__16"]/div/div/div[1]/div[4]'
all_elements = driver.find_element(By.XPATH, value=xpath)
for element in all_elements:
element.click()
sleep(2)
Important note!
id xpath has constantly changing and is generated by another on the google side
Tried to click on the class
class="r21Kzd"
Tried to click on the selector:
#qmCCY_adG4Sj3QP025p4__16 > div > div > div > div.wWOJcd > div.r21Kzd
Errors
This is when I try to click using xpath:
Message: no such element: Unable to locate element: {"method":"xpath","selector"://*[#id="vU-CY7u3C8PIrgTuuJH4CQ_9"]/div/div[1]/div[4]}
In other cases, the story is almost the same, the driver does not find the element and cannot click on it. Below I apply a scratch tag on which I need to click
screenshot tags on google search
Thanks for the help!
In case iDjcJe IX9Lgd wwB5gf are a fixed class name values of that element all you need is to use CSS_SELECTOR instead of CLASS_NAME with a correct syntax of CSS Selectors.
So, instead of driver.find_element(By.CLASS_NAME, "iDjcJe IX9Lgd wwB5gf") try using this:
driver.find_element(By.CSS_SELECTOR, ".iDjcJe.IX9Lgd.wwB5gf")
(dots before each class name, no spaces between them)

Cant find placeholder element using Selenium Python

I've trying to find the element 'Search' in the code above, but everytime I receive Unable to locate element. I already tried with xpath and css
<div class="wrapper-customer-search">
<search-customers class="hydrated">
#shadow-root (open)
<input type="text" placeholder="Search">
</search-customers>
you can try using io.github.sukgu that helps you to work on the shadow elements. I was able to automate the scenario that you mentioned. Below is the detailed code.
Step 1 add the below dependency
<!-- https://mvnrepository.com/artifact/io.github.sukgu/automation -->
<dependency>
<groupId>io.github.sukgu</groupId>
<artifactId>automation</artifactId>
<version>0.1.3</version>
</dependency>
Step 2 use the below import in the test file
import io.github.sukgu.*;
Step 3 Below is the entire code that worked for me
WebDriver driver = new ChromeDriver();
driver.get("<your_webpage>");
Shadow shadow = new Shadow(driver);
WebElement searchField = shadow.findElement("input[placeholder='Search']");
The <input> element with placeholder value as Search is within #shadow-root (open).
Solution
To locate the element you have to use shadowRoot.querySelector() and you can use the following solution:
Code Block:
time.sleep(3)
element = driver.execute_script("""return document.querySelector('search-customers').shadowRoot.querySelector("input[placeholder='Search']")""")
Reference
You can find a couple of relevant detailed discussions in:
How to handle the popup "Accepting all cookies" when the element is data-testid - Using Selenium in Python

How do I click a button using Selenium in Python?

I'm trying to crawl review data on an app from a Google Play website page using Python and Selenium. What I'm trying to do here is to click the "Full Review" button to see the entire text of each review.
But I keep running into these errors each time:
ElementNotInteractableException: Message: element not interactable
or
AttributeError: 'list' object has no attribute 'click'
or
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".LkLjZd ScJHi OzU4dc "}
The element for the button is this:
<button class="LkLjZd ScJHi OzU4dc " jsaction="click:TiglPc" jsname="gxjVle">Full Review</button>
The xpath for the button is this:
//*[#id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button
And this is the code I'm using for xpath:
full_review = driver.find_elements_by_xpath('//*[#id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button')
full_review.click()
I can't find the button by class name, xpath or anything.
Could anyone help me figure out this problem? In addition, is it possible to find the element by jsname?
I would really appreciate if you could help.
Avoid using xpath whenever possible, it's the most brittle selector.
id > CSS > Xpath
For your button, this css selector should work:
button[jsname='gxjVle']
You'll probably need to specify the child as that probably won't be unique
Your XPath selector is a little bit flaky, you can simplify it to be something like:
//button[text()='Full Review']
You're using the wrong function, if you're looking for a single unique element you should be using WebDriver.find_element_by_xpath one, mind that element, not elements
It's better to use Explicit Wait just in case element is not immediately available in DOM (for example if you're testing application built using AJAX technology)
Assuming all above:
full_review = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Full Review']")))

'Element Not Visible Exception' when trying to click on a Search option using webdriver selenium in python

I get an 'Element Not Visible Exception' when trying to click on a Search option on a webpage. The element is not hidden, and I have put a time.sleep(10) so the page has enough time to load. Please suggest why I am getting this error and how I can get around it.
I want to click on 'New Search' option in the code.
<a class="newsearch btn btn3d tbbtn" href="javascript:" style="position:static">
<div id="TBnewsearch"><img src="../../../../resources/images/mt_sprites.gif"
alt="New search" />
</div>
<span>New search</span>
</a>
Please find my code for clicking on it below :
time.sleep(10)
New_Search = browser.find_element_by_css_selector(' #Toolbar > table > tbody > tr > td.TBGroup.TBGroup1 > a.newsearch.btn.btn3d.tbbtn')
action2 = ActionChains(browser)
action2.move_to_element(New_Search).click()
action2.perform()
I've also tried doing a simple find and click on the element but get the same exception at the New_Search.click() step.
time.sleep(15)
New_Search = browser.find_element_by_xpath('//*[#id="Toolbar"]/table/tbody/tr/td[2]/a[1]')
New_Search.click()
I've tried using WebDriverWait as suggested by Debanjan below, but the expected condition isn't satisfied and I get a timeout exception.
time.sleep(15)
WebDriverWait(browser, 15).until(EC.visibility_of_element_located(browser.find_element_by_xpath('//*[#id="Toolbar"]/table/tbody/tr/td[2]/a[1]')));
New_Search.click()
Can you test these locators in the Dev tools and check if they are pointing to the right element on the page? In the console use $x() to test XPaths and $$() to test CSS selectors. If it returns 0, you know there's something wrong with your locator. If it returns 1, you are good to go. If it returns more than 1, you will need to verify that the element you are looking for is the first element returned. If it's not, you will need to craft a new locator. I have a feeling that your current locators is returning 2 nodes, out of which the first one is invisible.
Try to access the target element using that elements ancestors. Keep going one level up till you find a node that is uniquely identifiable and then use relative xpath (//) to traverse down to your element. You can also use ordinals/index to locate the element, but i would not recommend it as they will make your test brittle. If possible update the question with a bigger snippet of the HTML or the applications URL

Python Selenium: Can't find element by link text

I am trying to select "seltarget" from a list of other options.
HTML:
<div class="filter-item" data-reactid=".3.1.0.0.1.0.0.3.0.0.0.3">seltarget</div>
When exported to a python file it recommends that I locate the element by xpath:
driver.find_element_by_xpath("//div[#id='quickswitcher']/div/nav/ul/li/div/div/menu/div[2]/div/div/div/div[5]").click()
This is working for the time being because "seltarget" just happens to be 5th in the list. The above line will select whichever option is 5th in the list. I want to be more specific and select the element "seltarget"
Locating by link_text works only with <a> tags. To locate this element you can use the class if it unique
driver.find_element_by_class_name('filter-item').click()
Or the data-reactid attribute
driver.find_element_by_css_selector('[data-reactid*="3.1.0.0.1.0.0.3.0.0.0.3"]').click()
To locate by the text you can also use css_selector
driver.find_element_by_css_selector('div:contains("seltarget")').click()
Or xpath
driver.find_element_by_xpath('//div[contains(text(), "seltarget")]').click()
If you want to find the div by its text, you can use the following.
driver.find_element_by_xpath("//div[text()='seltarget']")

Categories

Resources