Selenium wait not solving error "Element is not currently visible" - selenium

I got an error:
Element is not currently visible and so may not be interacted with
Command duration or timeout: 63 milliseconds
So I added "wait" object to wait for the html obj to load:
val wait: WebDriverWait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("company_id")))
val selectCompany = new Select(driver.findElement(By.id("company_id")))
selectCompany.selectByValue("975")
but still i get the error:
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed
out after 10 seconds waiting for element to be clickable: By.id:
company_id
and I dont get it since the html is:
<select class="upload_company_id jcf-hidden" id="company_id" name="company_id" size="2"><option value="">Select account...</option>
<option value="100">100</option>
<option value="101">101</option>
<option value="104">104</option>
<option value="975">105</option>
this is more of the html:
thanks

I am not sure that the expected condition you choosed is right.The class you are using is select class. Could you please try elementToBeSelected(WebElement element)?

What you need to check is the computed CSS values of the element you are clicking. (In Chrome web inspector select the Computed tab on the right).
Selenium thinks that an element is hidden and can't be interacted with if any of these CSS values are set for the element:
display=none or
visibility=hidden or
visibility=collapse
If you can find any of these values on the computed tab, you will have to use JS to click on the element, or change the CSS value of the element on some way - Selenium will refuse interacting with the element regardless of the actual visibility of it. (I suspect the problem will be the "jcf-hidden" class)
When inspecting the element, switch to this tab in Chrome inspector on the right side:
and look for any of the mentioned values in the list.

Related

Selenium Python css selector by class doesn't work

I have 4 ways to locate some element and want to click on it:
DOM is:
<div class="ui dropdown selection" tabindex="0">
And I locate this element by four ways:
(By.XPATH, "//div[#class='ui dropdown selection']")
(By.CSS_SELECTOR, "[class='ui dropdown selection']")
(By.CSS_SELECTOR, ".ui dropdown selection")
(By.CLASS_NAME, "ui dropdown selection")
I i just clik on element
Way 1 and 2 work, test is ok - and len(element) is 1
Way 3 and 4 don't work: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ui dropdown selection"} - and len(element) is 0
(Waits don't help, and Way 1, Way 2 don't require waits at all)
Could you tell me why Way 3 and Way 4 failed ?
When using multiple class names for the same tag within a CSS Selector, they must be separated by a dot instead of a space. Here's the correct way to express your third one:
(By.CSS_SELECTOR, ".ui.dropdown.selection")
OR
(By.CSS_SELECTOR, "div.ui.dropdown.selection").
As for the fourth one, you can't use By.CLASS_NAME with multiple class name components. You would have to pick one, but since that probably won't give you a unique selector, you'll be better off using one of the other ways to form a selector.
#Michael
(By.CSS_SELECTOR, ".ui.dropdown.selection")
(By.CSS_SELECTOR, "div.ui.dropdown.selection")
Both don't work - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
But notice that there is only 1 element because this works:
(By.XPATH, "//div[#class='ui dropdown selection']")
and len(element) = 1

Robot Framework Locator not able to find locator

I have been trying to find the locator for my testing in robotframework. Below is my html element block and I am unsure of what is the "stable" locator and test on this. I would sincerely appreciate the xpath or any stable locator.
<div class="sc-jXcwIi bnswfh cosmos-layout-child" data-cosmos-key="stack-layout.item"><div data-cosmos-key="row-layout" class="sc-bQCGiA fFRDIw"><div class="sc-fXgAFM cosmos-layout-child" data-cosmos-key="row-layout.item">deploy-extension</div><div class="sc-fXgAFM cosmos-layout-child" data-cosmos-key="row-layout.item"><span data-cosmos-key="text" type="allcaps" class="sc-fujyUd cWWTbP">MACHINE TO MACHINE</span></div></div></div>
I have tried the locator
Click Element xpath://div[contains(text(), "deploy-extension")]
But this is giving me error.
export existing tenant | FAIL |
ElementClickInterceptedException: Message: element click intercepted: Element <div class="sc-fXgAFM cosmos-layout-child" data-cosmos-key="row-layout.item">...</div> is not clickable at point (431, 355). Other element would receive the click: <p data-cosmos-key="paragraph" class="sc-dlnjPT corHA">...</p>
(Session info: headless chrome=87.0.4280.66)
focus on the element first.
Set Focus To Element xpath://div[contains(text(), "deploy-extension")]
you can try with various locators as below,
//div[contains(text(), "deploy-extension")]//following::span[contains(text(),'MACHINE TO MACHINE')]
//span[#text='allcaps'and contains(text(),'MACHINE TO MACHINE')]
https://www.tutorialspoint.com/what-is-xpath-in-selenium-with-python

Selenium cannot find element by ID nor xpath

I'm trying to write a script in python (3.7.3) to automate logging into a website by using Selenium for the first time. I practiced with some basic examples and went through the Selenium documentations. All good so far. But when I try it on a website of my own choice; things go wrong...
I'm managing to open up the login page, but whenever I try to get the element ID corresponding to the username field, I'm getting the "NoSuchElementException", indicating that the ID-name I'm using is supposedly incorrect. I'm getting the name by looking at the HTML code by right clicking in the username-box and using the inspect function. When this is not working, I'm trying to find it through xpath, but also without success. Can anyone point out why the element is not being recognized?
Python code
from selenium import webdriver
path = r"C:\Users\path\chromedriver.exe"
driver = webdriver.Chrome(path)
driver.get ("https://a website")
driver.find_element_by_id("login-username").send_keys(login)
driver.find_element_by_id("login-sign-in-button").click()
Error message
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="login-username"]"}
HTML code of the username field:
<input id="login-username" type="text" name="username" placeholder="Username" msd-placeholder="Username" class="margin-bottom form-control ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" ng-model="formData.username" dh-autofocus="" required="">
Looking for element with xpath. I've changed the "" brackets around the id by '' to avoid errors.
driver.find_element_by_xpath("//*[#id='login-username']").send_keys(login)
And finally I tried the long xpath
driver.find_element_by_xpath("/html/body/ui-view/ui-view/div/div[1]/div[1]/ui-view/div/div[1]/div/div[2]/form/div[1]/div/input").send_keys(login)
I'm honestly hitting a brick wall here. It's probably not helping my knowledge of HTML is practically non existing.
EDIT 1
Added wait function. Code works now
driver.get ("https://a website")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login-username")))
driver.find_element_by_id("login-username").send_keys(login)
Answering to close this question.
As Alok pointed out, we need to wait for the webelement to get loaded completely before trying to access it.
driver.get ("https://a website")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login-username")))
driver.find_element_by_id("login-username").send_keys(login)

find specific element for button using selenium for web scraping

I am inspecting one button element from a web page using chrome driver and selenium. And the html code for the particular button is:
<div class="label text-left text-link link-blue text-
uppercase">Financial Statement Analysis <span class="count">(2)</span>
</div>
I have tried different element options like find element by name, xpath, link text etc. But none of them unable to locate the element.
What will be the element to locate the button. ?
try Xpath :
//span[contains(#class,'count') and text() = '(2)']
You can try with this css selector :
div.label.text-left.text-link.link-blue.text-.uppercase
To locate the element with text as Financial Statement Analysis (2) you can use the following solution:
Java Solution:
WebElement elem = driver.findElement(By.xpath("//div[#class='label text-left text-link link-blue text-uppercase'][contains(.,'Financial Statement Analysis')]"));

Selenium wait for element to appear and dissapear

I have the following element:
<div class="ui-helper-hidden" id="shell.indicator.busy">
<label>Processing...</label>
<br />
<img src="/RightCrowd/Images/loading.gif" alt="" />
</div>
And it appears like this:
I would like to wait for it to appear and then to disappear and continue with accessing elements. This appears on most pages of the web application. We've tried a few things but sometimes it is visible by Selenium and sometimes is not, although I can see it with my eyes.
I believe that this processing image appears on top of the current page and using the current handler may be useless. Not sure.
We've tried something like this:
WebElement element = (new WebDriverWait(webDriver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//img[contains(#src,'/Images/loading.gif')]")));
boolean processingEnd = (new WebDriverWait(webDriver, timeoutWaitForProgressbar))
.until(ExpectedConditions.invisibilityOfElementLocated(By.id("shell.indicator.busy")));
So we've tried both xpath and id... Please let me know what's the best way to handle this situation.
By default, the WebDriverWait would check the Expected Condition status every half a second. I would try to issue the expected condition check requests more often with a FluentWait class:
Wait wait = new FluentWait(driver)
.withTimeout(timeoutWaitForProgressbar, SECONDS)
.pollingEvery(100, MILLISECONDS);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("shell.indicator.busy")));