How to find href through class name and click with selenium - python

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']

Related

How to do double click on <a> tag using python selenium

I want to scrape the data on web site. But I have a problem, I want to click the a tag with double click. Who know about this? If you know this, help me, please.
My code:
<a href="#product-lists id='products'">
If you know about it, Please help me.
Thanks
path='//a[#href="{}"]'.format("#product-lists id='products'")
print(path)
from selenium.webdriver import ActionChains
source = driver.find_element_by_xpath(path)
action = ActionChains(driver)
action.double_click(source).perform()
Your href value looks really messed up but to double click an element by their href values use action builder and find the href value with that value.

Facebook post_box element. I cant find element by xpath

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

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()

How to send keys to a difficult element in selenium (Python)

I am trying to click a small button, which has no "ID" or "Name", on a website. The only unique identifier is onclick, which is as follows:
onclick="submitForm('DefaultFormName',1,{'param1':'HXCTIMECARDACTIVITIESPAGEXgm7J5oT','serverValidate':'1uCdqvhJe','param2':'',event:'details|1'});return false;"
However, another button on the page has the following onclick:
onclick="submitForm('DefaultFormName',1,{'param1':'HXCTIMECARDACTIVITIESPAGEXgm7J5oT','serverValidate':'1uCdqvhJe','param2':'',event:'details|2'});return false;"
The only difference is a 1 vs. a 2 in the middle of the code. I tried to use a "find_element_by_css_selector" with the following code but it didn't work:
driver.find_element_by_css_selector(".x1p[onclick*='details|1']").click()
Another option would be selecting the preceding element, with the following code:
precedingbutton = driver.find_element_by_name('B22_1_6')
And then sending tab and then enter. However, after I send Tab, I don't know of a way to send Enter without assigning the Send_Keys command back to the preceding box, which deselects the button I want.
Let me know if you can help!
If you can locate the parent, you can locate the child, either with xpath or the nth-child css selector, relative to it.
Edit in response to comments:
Assuming by "2 elements away" you mean it is a sibling preceding the target, try something like td[name="B22_1_6"] + td + td. This is the sibling selector.
You can do it easily by
driver.find_element(By.XPATH, 'your xpath').click()
For finding the xpath , just inspect the button with firebug, and in html tab right click on selected element and click on Copy XPath.
Hope it will help you.

Selenium Webdriver with Python - obtaining the string or contents in between html tags

I'm new to Selenium's webdriver and Python. I know about this article about getting the HTML source, but I don't want the entire HTML line for a DOM object, just the content in between the tags. I also looked at webdriver source code for help on finding this button DOM object
for example:
<button id = "picview">Pic View</button>
How do I just get "Pic View"?
Also, using get_attribute("button id"), How would I get this specific button id as there are multiple buttons on the page with button id?
For example:
picbox_elem_attr = picbox_elem.get_attribute('button id')
print picbox_elem_attr
How do I ensure that picbox_elem_attr variable is set to the "picview" button and not some other button?
I don't have a
driver.find_element_by_name("xxx")
or a
driver.find_element_by_id("aaa")
to use to find this button.
To get text of an element use the text property. For example:
text_inside_button_id = driver.find_element_by_id("picview").text
Here is some additional documentation to help you with the webdriver binding library.
http://goldb.org/sst/selenium2_api_docs/html/
I forgot about xpath. Oops!
driver.find_element_by_xpath('//*[#id="picview"]')
and then you can right-click the object and use xpath within the dev tools.

Categories

Resources