Selenium cannot find div inputfield element - python

<div class="writtingArea _editor" contenteditable="true" placeholder="보낼 메시지를 입력하세요."></div>
it is element that i needed to find and send chat but if i try to find this with this line of code,
driver.find_element_by_class_name("writtingArea _editor").click()
it gives me error that says it couldn't locate element. So i tried to locate it by Xpath. but it didn't work either.
How can i fix this??

Since there is a space between two class names you are not able to call that class names merged.
Please try below line to find and click that element.
driver.find_element_by_css_selector("div.writtingArea._editor").click()

Related

I have "no such element: Unable to locate element" error although ID is 100% correct

I am trying to click on "OK" button on a pop up using selenium and Python but i face an error "no such element: Unable to locate element" although being sure that my id is 100% correct.
> <a class="dxm-content dxm-hasText dx dxalink" href="javascript:;" role="menuitem" id="Dialog_PAC_Menu_DXI0_T"><span class="dx-vam dxm-contentText">OK</span></a>
My python selenium code:
Export2 = driver.find_element(By.XPATH,'//a[#id="Dialog_PAC_Menu_DXI0_T"]')
Export2.click()
Where exactly did I go wrong, i also tried full Xpath, wait till clickable, time sleep. everything!
i would appreciate if someone can help me with it.
Well, it looks like you found the answer and that was because your element was inside an iFrame so I will just post an answer here so others can find an easy answer if they view your question.
tmpheader = driver.find_element(By.CSS_SELECTOR, "#modal > iframe")
driver.switch_to.frame(tmpheader)
Export2 = driver.find_element(By.XPATH,'//a[#id="Dialog_PAC_Menu_DXI0_T"]')
Export2.click()
This switches our current driver that we are looking for into the first iFrame element on the page. If there is more than one iFrame you would need to check for more attributes in the first line to make sure you are looking at the correct one
Actually that's how I solved my issue as my element was inside an iFrame as John Gordon suggested.
driver.switch_to.frame(driver.find_element(By.TAG_NAME,'iframe'))
slider = driver.find_element(By.XPATH,"//*[contains(#id,'_xaf_dviImportExportFormat_Edit_dropdown_DD_B-1Img')]").click()

Issue in locating web element selenium

I am unable to locate a web element on a website, The web elements of the website are dynamic and the elements which i am trying to locate have very similar attributes to that of others with just small differences like changes in integers. These integers also change when i refresh the page so i am unable to locate can someone help?
I tried following but there maybe mistakes in syntaxes:
With contains text = WebDriverWait(browser, 15).until(EC.presence_of_element_located( (By.XPATH,"//div[contains(text(),'Type here...')]") ))
Absolute Xpath and Rel Xpath(these changes)
Contains sibling, contains parents but the parent and sibling elements are also unable to locate
here is the html of the element <div contenteditable="true" class="cke_textarea_inline cke_editable cke_editable_inline cke_contents_ltr placeholder cke_show_borders" tabindex="0" spellcheck="true" role="textbox" aria-label="Rich Text Editor, editor15" title="Rich Text Editor, editor15" aria-describedby="cke_849" style="position: relative;" xpath="1">enter question here</div>
There are spaces being added () which some time cause issue, please deleting that space. Also if you can share html then we might be able to help. WebDriverWait(browser,15).until(EC.presence_of_element_located((By.XPATH,"//div[contains(text(),'Type here...')]")))
I removing space doesn't work then you can try below xpath-
//div[text()='enter question here'][contains(#aria-label,'Rich Text Editor')]
If there are really no attributes you would consider reliable, you can access the element by the text inside of it. I'd recommend two things: Don't do this unless you find no other choice, and don't just use //div, add as much XPATH info to the path as you can.
driver.find_element_by_xpath('//div[text()="enter question here"]')
OR
driver.find_element_by_xpath('//div[contains(text(),"enter question")]')

How to get value of onclick using xpath?

I have this piece of Html:
<tr class="selectable" onclick="PesquisarProntuarioView.EditarProntuario('108077098085')">
I would like to get what is inside onclick, i was looking for a command which would give me:
"PesquisarProntuarioView.EditarProntuario('01048108077098085')"
I already have tried several commands like this one:
element=driver.find_element_by_xpath("//a/tr/#onlick=PesquisarProntuarioView.EditarProntuario(*)")
driver.get(element)
However still no clue, Could someone please help me with the respective command in Selenium/Python?
Locate the element and use get_attribute() method:
element = driver.find_element_by_xpath("//a/tr")
print(element.get_attribute("onclick"))
If there are multiple elements you need to extract onclick from, use find_elements_* and call get_attribute() for every element found:
for element in driver.find_elements_by_xpath("//a/tr"):
print(element.get_attribute("onclick"))
I would slightly change the above solution to only select relevant elements in path //a/tr by providing part of the onclick attribute, like this:
onclickValue = driver.find_element_by_xpath("//a/tr[contains(#onlick,'PesquisarProntuarioView.EditarProntuario')]").get_attribute("onclick")

Selenium CSS_Selector looking for an element with specific text is turning out a 'NoSuchElementException'

I'm having an issue with looking for an element with a particular text attribute utilizing CSS_Selectors in Selenium. Here is the current line of code I have:
element = driver.find_element(By.CSS_SELECTOR, "li.adTypeItem[text='CLASS']")
I've had trouble using the attribute selector brackets in CSS_Selectors in the past, and clearing this up would really go a long way to better understanding how to use CSS_Selectors in the future.
Please note - i'm not looking for a element with a class, but rather the actual text that is displayed with that element.
Is this the only place on the page with the text "class" displayed? If so you can try:
driver.find_element(By.LINK_TEXT('CLASS'));
driver.find_element(By.PARTIAL_LINK_TEXT('CLASS'));

Using Python and Selenium why am I unable to find link by link text?

I have a list webelement that has a bunch of links within it. The html looks like:
<li>
<span class="ss-icon"></span> Remove
<a href="/sessions/new"><span class="ss-icon"></span> Sign in to save items</a
...
When I try to do something like:
link = element.find_element_by_link_text('Sign in to save items')
I get an error that says:
NoSuchElementException: Message: Unable to locate element:
{"method":"link text","selector":"Sign in to save items"}
I have been able to find this link by instead doing a find_elements_by_tag_name('a') and then just using the link with the correct HREF, but I would like to understand why the first method fails.
It happened to me before that the find_element_by_link_text method sometimes works and sometimes doesn't work; even in a single case. I think it's not a reliable way to access elements; the best way is to use find_element_by_id.
But in your case, as I visit the page, there is no id to help you. Still you can try find_elements_by_xpath in 2 ways:
1- Accessing title: find_element_by_xpath["//a[contains(#title = 'Sign in to save items')]"]
2- Accessing text: find_element_by_xpath["//a[contains(text(), 'Sign in to save items')]"]
Hope it helps.
The problem is, most likely, in the extra spaces before or/and after the link text. You can still approach it with a "partial link text' match:
element.find_element_by_partial_link_text('Sign in to save items')

Categories

Resources