Accessing data from class selenium - python

Currently trying to get the data which is stored in this class using selenium and print it into console.
<h3 class="tiktok-dvof16-AuthorTitle e10yw27c0">allawi_9</h3>
I tried print(driver.find_elements_by_class_name("tiktok-dvof16-AuthorTitle e10yw27c0"))
I also tried adding .text at the end, however you cant do that on a list obj. (And the returned list is empty may I add.
However, it just returns an empty list. I'm unsure what I'm doing wrong. Any help would be great!

if the element value is unique on the page you can use
myelemnt = driver.find_element_by_name('Some text')
myelement.print()
if not you can use X-path for this like
myelement = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a/div/div[2]')
myelement.print()
if that doesnt worked use Full x-path
myelement = driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/div[2]/article/div[1]/div/div[1]/div[1]/a/div/div[2]')
myelement.print()
for getting the x-path/full x-path simple just right-click on the element on the google chrome developer tools and click on Copy then x-path / full x-path

Related

xpath to check only within WebElement in Selenium / Python

I am very new to this and i have tried to look for the answer to this but unable to find any.
I am using Selenium+chromedriver, trying to monitor some items I am interested in.
Example:
a page with 20 items in a list.
Code:
#list of items on the page
search_area = driver.find_elements_by_xpath("//li[#data-testid='test']")
search_area[19].find_element_by_xpath("//p[#class='sc-hKwDye name']").text
this returns the name of item[0]
search_area[19].find_element_by_css_selector('.name').text
this returns the name of item[19]
why is xpath looking at the parent html?
I want xpath to return the name of item within the WebElement /list item. is it possible?
found the answer, add a . in front
hope this is gonna help someone new like me in the future.
from
search_area[19].find_element_by_xpath("//p[#class='sc-hKwDye name']").text
to
search_area[19].find_element_by_xpath(".//p[#class='sc-hKwDye name']").text
What you are passing in find_element_by_xpath("//p[#class='sc-hKwDye name']") is relative Xpath. You can pass the full Xpath to get the desired result.

Can't locate element on google chrome webpage

I'm trying to change the chrome profile name on this like : chrome://settings/manageProfile using Python and Selenium :
It's the empty textbox on the top left corner:
The issue is that I can't access to the element, I tried all the stuff below :
chromeProfilName = browser.find_element(By.XPATH, "//*[#id='profile-name']")
chromeProfilName = browser.find_element(By.ID, "profile-name")
chromeProfilName = browser.find_element(By.ID, "input")
I don't really understand how the HTML page is made, but when I examine the page, I found the textboxID = "input". However, the value is stored in a span, which ID is "profile-name".
I always have the same issue : "no such element: Unable to locate element". I don't have a deep knowledge about Selenium. I already looked for answers on internet but I found nothing.
Thanks !
To access the input element you need traverse through shadowroot element.Use the following querySelector to indentify the input tag.
driver.get("chrome://settings/manageProfile")
profileInput = driver.execute_script('return document.querySelector("settings-ui").shadowRoot.querySelector("settings-main").shadowRoot.querySelector("settings-basic-page").shadowRoot.querySelector("settings-people-page").shadowRoot.querySelector("settings-manage-profile").shadowRoot.querySelector("cr-input").shadowRoot.querySelector("#input")')
profileInput.click()
profileInput.clear()
profileInput.send_keys("user676767")
Browser snapshot:
The accepted answer works, I just want to add something :
For those who thought you've to search yourself the path with shadowroots, in reality you don't have to.
With Chrome for exemple (it's not possible on Firefox), when you target the element in the source code, copy the "JS path". Then you just have to paste the path in the execute_script function.

How to get full X-path of element instance using selenium python

I wanted to get full X-path of element instance using selenium python ?
Input:
element_inst = driver.find_element_by_link_text('Next')
I wanted to get full X-path of element_inst
Sample Output:
/html/body/div[1]/div[6]/div[1]/div[1]/div[1]/content-viewer/div/div/div/div[2]/div[2]/div/activity-viewer/div/div/phase-map-directive/div/div/div/ul/li[3]/div/span[1]
is there any way to print full X-path of element?
Option -1
I found below link which may help you:
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5520
Option 2-
You can use Sikuli and record code for below steps and use clipboard data as xpath in your code
Right Click and click on inspect
On selected element just right click
Copy-> Xpath
Hope this helps

Click on li element searching specific displayed text

Using python 3 and chrome driver. I'm trying to click on my desired element searching for the text displayed on this page . For example, in case of "BEBES" I'm using:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[contains(text(), "BEBES")]'))).click()
but nothing happens. Just throws the time out exception. What's my error?
Your xPath is not correct. Use this:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[contains(text(), "Bebes")]'))).click()
Note: upper/lowercase makes difference
and
This post suggests using the following as text() returns a node set:
//*[text()[contains(.,'BEBES')]]
XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

Using selenium to get access class info on website

I am using the following code using Python 3.6 and selenium:
element = driver.find_element_by_class_name("first_result_price")
print(element)
on the website it is like this
`website: span class="first_result_price">712
however if I print element I get a completely different number?
Any suggestions?
many thanks!!
"element" is a type of object called WebElement that Selenium adds. If you want to find the text inside that element, you have to say
element.text
Which should return what you're looking for, '712', albeit in string form.

Categories