Facebook post_box element. I cant find element by xpath - python

Facebook post_box element. I cant find element by xpath
Hi,
I learn to write automation test scripts in Selenium Webdriver with Python. I will make "Facebook Poster script". After login i wanna click in SendBox and sent some text, but i cant identyfing it with Xpath. Firebug not help mi with this. Thx for help.
Something like this:
After login
newpost=driver.find_element_by_xpath("i dont know correct xpath")
newpost.click()
newpost.send_keys("Hello!")
newpost.submit()
Xpath from Firebug not working
newpost = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div[1]/div/div[2]/div[2]/div[2]/div/div[3]/div/div/div/div[2]/div[1]/div/div/div/div[2]/div/div[1]/div[1]/div[2]/div/div/div/div/div/div[2]/div/div/div/div")

First of all, never use absolute xpath - it's costly to maintain and looks just horrible.
As I understand, you need to click on input field with the placeholder 'what's on your mind'. Then you can get use of 'data-testid' attribute with value 'status-attachment-mentions-input'.
If you really need to use xpath, try this:
"//[#data-testid='status-attachment-mentions-input']". You can also use css selector and find yo element like this:
inputField = driver.find_element_by_css_selector("[data-testid='status-attachment-mentions-input']")

Correct solution:
newpost = driver.find_element_by_css_selector("[name='xhpc_message']")
newpost.click()
newpost.send_keys("Hello!")
newpost.submit()
#GVi thx for help

Related

Selenium - Get element after a specific element (Python)

As you can see from the picture, the pagination is a bit different. So my idea is to get the position of the current-page and get the link for the a tag.
current_page= driver.find_element(By.CSS_SELECTOR,"span.current-page")
driver.find_element(By.TAG_NAME,"a").click()
but I didn't know to use the current_page element to find the a tag after it and click on it.
Thanks for your help in advance.
To get the next element using current page reference you can use the following css selector in one liner or XPATH option.
next_page= driver.find_element(By.CSS_SELECTOR,"span.current-page+a")
print(next_page.get_attribute("href"))
next_page.click()
Or you can use this.
current_page= driver.find_element(By.CSS_SELECTOR,"span.current-page")
current_page.find_element(By.XPATH,"./following::a[1]").click()

Element not found when using webdriver with Selenium in Python

got a problem with my code and I need your help. What I'm trying to do is the following:
1- access a website;
2- fill the registration form: name, email, password, etc.
Step 1 works; after clicking the sign up button, the form will pop up in a new tab.
Step 2; when trying to find the elements, by, id or name, I get the error "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element"
My code is the following:
driver.find_element_by_link_text('Sign Up').click()
time.sleep(3)
driver.find_element_by_id("signup_title").send_keys(signup_tile)
driver.find_element_by_id("signup_name").send_keys(signup_name)
Attached you can find the html. Thank you in advance, much appreciated your help.
Note
In console I tried to find the element searching the id using
$x("//*[#id='signup_title']") and it was found: [input#signup_title.sc-AxirZ.kzqQJb.invalid]. Also the element is loaded.
HTML
Try using:
driver.find_element_by_xpath("<XPath>")
It seems to work better. To get the XPath of an element, find the element in the inspector, right-click it then Copy -> Copy XPath. If that doesn't work, select Copy Full XPath instead
The problem was related to the second tab; because the form opens in another tab, the window must be switched in the code.
I used the following:
driver.switch_to.window(driver.window_handles['Nr']), Nr being the index for the tab- if there are 2, the main one, and the second one-in my case with the form, the index will pe 1-counting starts from zero.

How to find href through class name and click with selenium

I'm using Selenium with python to make a spider.
A part of the web page is like this:
text - <span class="sr-keyword">name</span> text
I need to find the href and click.
I've tried as below:
target = driver.find_element_by_class_name('_j_search_link')
target.click()
target is not None but it doesn't seem that it could be clicked because after target.click(), nothing happened.
Also, I've read this question: Click on hyperlink using Selenium Webdriver
But it can't help me because in my case, there is a <span class>, not just a simple text Google.
You can look for an element with class _j_search_link that contains text Wat Chedi Luang
driver.find_element_by_xpath('//a[#class="_j_search_link" and contains(., "Wat Chedi Luang")]')
driver.find_elements_by_partial_link_text("Wat Chedi Luang").click()
I think you didn't find right element. Use CSS or Xpath to target this element then click it, like:
//a[#class='_j_search_link']
or
//a[#class='_j_search_link']/span[#class='sr-keyword']

Unable to locate element python selenium - Need to click this button

So, this button has no id/name and I've tried xpath, class name, CSS selector, etc
Here is the HTML code:
Thanks in advance.
Be sure to switchTo the iframe that contains this button! Then I think that this CSS path should work:
div.submit-and-reset-wrapper button.continue
You can try a kind of ugly hack:
get all the button/span elements
loop all the button/span elements until you find the text.
then click the element...
something like:
all_span_elements = driver.find_elements_by_tag_name("span")
for span_element in all_span_elements:
if "Begin Lesson" in span_element.text:
span_element.click()

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