Cant figure out how to write xpath in python in selinium - python

I have got this piece of html code:
<span class="amount ng-binding ng-scope" ng-if="object.numberOfReactions !== null">41</span>
I want to acces the 41 number by using driver.find_elements_by_xpath,
my best try is
driver.find_elements_by_xpath(//div[#ng.if='object.numberOfReactions !== null'])but this gives an empty list []. Help would be greatly appreciated.

I know that there are multiple ways to write the xpath, but if you go to the html code, highlight it, and right click you should see options that allow you to copy the full xpath. I personally use this alot because it's really fast and easy to use

Xpath is the property to element in browser. It doesn't mater which language we are using. XML path will be the same. Use to find xpath technique.
In python we can find element using driver.find_elements_by_xpath

Related

Selenium Href Lookup Issues Python

I am currently writing a script for browser automation using selenium and I need to click a button on a webpage. I have successfully found and identified a bunch of buttons to press using driver.find_element(By.ID,'name').click() but when I get to one of the buttons I need to press there is no ID and this is the full element: <a tabindex="-1" href="/miscOperations/theWall?dash=true">The Wall</a>
I am running into issues using the By.XPATH function of find element because of the '?' within the href. Is there anyway to search for the string "The Wall" and find the element? I think this would be my solve since it is a unique ID within the HTML but I am not sure how to find a certain string within the HTML. Another way I could possibly do it is to find a way to put the '?' character into Python without causing a syntax error due to the character itself even though its within a string.
Do you guys have an idea on the best way to go about this?
Thanks so much!
driver.find_element(By.LINK_TEXT,"The Wall").click()
It's in an a tag so you can use Link text.

Python Selenium Finding Element With Dynamic URL and Name with Little HTML

Hey everyone! Today I am working with Python 3 and Selenium web driver. I am encountering a problem with finding by element. Here is the HTML:
<a _ngcontent-pxc-c302="" class="name ng-star-inserted" href="/person/20d4a795d3fb43bdbee7e480df27b05b">michele regina</a>
The goal is to click on the first name that appears in a listed column.
The name changes with every page refresh. This comes with two problems in that the link text is the name and it changes, and the link constantly changes as well for each different name except for the part that says /person/
I tried the following:
driver.find_element_by_css_selector("a.name ng-star-inserted").click()
driver.find_element_by_class_name("name ng-star-inserted").click()
Which resulted in an element that is not a clickable error.
I also tried xpath by copying and pasting the entire XPath copied from Google inspector... lol
driver.find_element_by_xpath('/html/body/app-root/app-container/div/div/tc-app-view/div/div/div/ng-component/mat-sidenav-container/mat-sidenav-content/div/div[2]/tc-person-table-container/tc-collapse-expand[1]/section/tc-person-table/table/tbody/tr[1]/td[2]/tc-person-table-cell/a').click()
and it kind of works sometimes, which is weird but I am sure there has to be a better way. Thank you so much in advance! Any help is appreciated! Also, I am very new to Python, I know zero HTML, and I am also newer to stack! Thanks!
From the given full xpath the a tag you are looking for is in table,section,div. Look for an id in those tags and trace the a.

Can't find twitter DM textbox by xpath using selenium

I am trying to find the textbox element using the find_element_by_xpath() method, but It keeps telling me it cant find said element, here's the line of code that does that.
I've tried finding it by link_text, partial link text, selector and it just doesn't work
bar = nav.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/div/div[2]/div/div/aside/div[2]/div[2]/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div')
Thanks in advance!
So, I suggest creating your xpath if you want to be precise and avoid taking it based on html structure (which can change).
The locators looks like:
And you can take it with xpath:
//input[#placeholder='Search people' and #role='combobox']
To avoid this problem, I suggest going trough a tutorial for a better understanding regarding how to create custom locators: Xpath tutorial
What was happening is: When I opened the tab to inspect the element, the DM structure changed because of my screen size so the xpath wasn't the same

Xpath issues with content egg plugin

I don't usually post on here as you all seem pretty advanced for me, but this feels like an advanced question, so thought I'd ask.
I'm using the Content Egg plugin for WordPress (please don't roll eyes) and it's asking me for XPath for a price I'm trying to get. I have tried so many different xpaths and none of them seem to be working. I'm trying to get the price Xpath from this webpage. Any ideas?
https://www.hockeydirect.com/Catalogue/Hockey-Sticks/Young-Ones-Hockey-Sticks/Young-Ones-ABD-Hockey-Sticks/Young-Ones-ABD-70-Hockey-Stick-341111
This is what i've tried:
//html/body/form/div[5]/div[2]/div[3]/div/span/div[1]/div[4]/div[2]/div[2]/div/div[1]/div/span/fieldset/div[1]/div/div[1]
//*div[#class="MainPriceContainer"]
//div[#class='MainPriceContainer vcenter']
Any help would be greatly appreciated.
try to copy the xpath from firefox by this steps
inspect elements and select the price with the selector pointer it will highlight the element in the inspector html
then right click to the highlighted code then copy then select XPATH from inside copy
then paste it to the Xpath
then set the price update in the offer module setting to 30 seconds and test
if worked tell me :)

Selenium/python 2.7 How to find element that only has a title?

im new to selenium and I am trying to click on an image but when I inspect the element it only has a title, an src=, and an alt=. I would like to use xpath but am unsure exactly how it works on python, on java i would do driver.findElement(By.xpath("//img[#title='']")) but I am unsure of how to do it on python any help is appreciated :)
In Python, there is the find_element_by_xpath() shortcut method:
driver.find_element_by_xpath("//img[#title='']")
Note that your locator does not look quite reliable. Providing the relevant HTML of the image and its parents might help us to provide you with a better option.

Categories

Resources