I can't figure it out how to find xpath using following-sibling for those marked in green in the pic. Basically I want to click on the button called "Open" which is on the same row as the text "front".
Try this XPath:
//td[strong="front"]/following-sibling::td//a[.="Open"]
P.S. As you didn't provide HTML sample as text (do not provide it as picture!!!) I can't see whether there is a text in <i>...</i> node
So you also can try
//td[strong="front"]/following-sibling::td//a[contains(., "Open")]
Try this
//strong/parent::td/following-
sibling::td/descendant::i/following-sibling::text()
[contains(.,'Open')]
The above xpath is assuming the level of Open text and i is of same level
You can use this XPath:
//td[strong="front"]/following-sibling::td//a[contains(text(), "Open")]
As per the HTML you have shared the element with text as front doesn't have any siblings. So you may not be able to use following-sibling.
Solution
To click on the button with text as Open which is on the same row with the element with text as front you you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='front']//following::td[1]//a[#class='btn default ']"))).click()
Using XPATH along with the text:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='front']//following::td[1]//a[contains(., 'Open')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Related
Is there any way to read the text in an input without calling to get it from HTML? In the image, I want to get whatever value I can from the input, store it, then add a number to it, but it's not in the HTML(the input just refers to "quantity"
Is there some way I could select the input box, copy the value, then interact with the value from there?
To print the text i.e. 1 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.validation-message-container > input[data-bind*='isForwardFreightSeller']"))).get_attribute("value"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='validation-message-container']/input[contains(#data-bind, 'isForwardFreightSeller')]"))).get_attribute("value"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You should be able to use the following javascript snippet for this:
return arguments[0].value;
This is assuming you know how to execute javascript on an element.
Trying to scrape odds from fanduel, goal is to get the player's name. In this case Jayson Tatum.
https://sportsbook.fanduel.com/basketball/nba/philadelphia-76ers-#-boston-celtics-31137202?tab=player-points
Even when I copy the xpath directly from chrome it doesnt seem to work. Though it works when I hardcode and look for an element through xpath containing the text Jayson Tatum.
This is my code
name = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH,'//*[#id="root"]/div/div[2]/div[1]/div/div[2]/div[3]/div/div[2]/div/div[3]/div[1]/div/div/div[1]/span')))
Also tried this
name = driver.find_element(By.XPATH, '//*[#id="root"]/div/div[2]/div[1]/div/div[2]/div[3]/div/div[2]/div/div[3]/div[1]/div/div/div[1]/span')
Still get a NoSuchElement trying both ways.
To print the text Jayson Tatum you can use the following Locator Strategy:
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//span[text()='UNDER']//following::div[1]//span").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategy:
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='UNDER']//following::div[1]//span"))).get_attribute("innerHTML"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium
I'm new on python, just started learning, I try to get value span on other website and I'm trying to get value on this picture on red mark , but I still get an error.
Snapshot of the HTML:
To print the text 4 hours 30 minutes you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "div.lab-preamble__details.subtitle-headline-1 > span").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//div[#class='lab-preamble__details subtitle-headline-1']/span").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.lab-preamble__details.subtitle-headline-1 > span"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='lab-preamble__details subtitle-headline-1']/span"))).get_attribute("innerHTML"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium
If you're using only selenium to find element you can try to get this element using find_element_by_xpath.
To get xpath fast and easy you can right-click element in Chrome DevTools and in "Copy" section there is the "Copy xpath" option.
I will try to make it really short. I am trying to click on a product that came out of a search from a website. Basically there is a list of matching products, and I want to click on the first one which contains the product name I searched in its title.
I will post the link of the website so you can inspect its DOM structure: https://www.tonercartuccestampanti.it/#/dfclassic/query=CE285A&query_name=match_and
In this case, many contain my query string, and I would simply like to click on the first one.
Here is the snippet of code I wrote for this:
def click_on_first_matching_product(self):
first_product = WebDriverWait(self.driver, 6).until(
EC.visibility_of_all_elements_located((By.XPATH, f"//a[#class='df-card__main']/div/div[#class=df-card__title] and contains(text(), '{self.product_code}')"))
)[0]
first_product.click()
The problem is that 6 seconds go by and it cant find an element that satisfies the xPath condition i wrote, but I cant figure out how to make it work.
I am trying to get a search result a element and check if the title it has down its structure contains the query string I searched.
Can I have some help and an explanation please? I am quite new to selenium and XPaths...
Can I please also have a link to a reliable selenium documentation? I am having some hard times trying to find a good one. Maybe one that also explains how to make conditions for xPaths please.
You need to consider a couple of things. Your use-case would be either to click on the first search result or to click on the item with respect to the card title. In case of clicking on a definite WebElement inducing WebDriverWait for visibility_of_all_elements_located() will be too expensive.
To click on the item with respect to the card title you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategies:
Using the text CE285A Toner Compatibile Per Hp LaserJet P1102 directly:
driver.get('https://www.tonercartuccestampanti.it/#/dfclassic/query=CE285A&query_name=match_and')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='CE285A Toner Compatibile Per Hp LaserJet P1102']"))).click()
Using a variable for the text through format():
driver.get('https://www.tonercartuccestampanti.it/#/dfclassic/query=CE285A&query_name=match_and')
text = "CE285A Toner Compatibile Per Hp LaserJet P1102"
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='{}']".format(text)))).click()
Using a variable for the text through %s:
driver.get('https://www.tonercartuccestampanti.it/#/dfclassic/query=CE285A&query_name=match_and')
text = "CE285A Toner Compatibile Per Hp LaserJet P1102"
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='%s']"% str(text)))).click()
To click on first search product you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
CSS_SELECTOR:
driver.get('https://www.tonercartuccestampanti.it/#/dfclassic/query=CE285A&query_name=match_and')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.df-card>a"))).click()
XPATH:
driver.get('https://www.tonercartuccestampanti.it/#/dfclassic/query=CE285A&query_name=match_and')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='df-card']/a"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Your xpath seems incorrect.Try following xpath to click on product.
driver.get("https://www.tonercartuccestampanti.it/#/dfclassic/query=CE285A&query_name=match_and")
def click_on_first_matching_product(product_code):
first_product = WebDriverWait(driver, 6).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[#class='df-card__title' and contains(text(), '{}')]".format(product_code))))[0]
first_product.click()
click_on_first_matching_product("CE285A")
I am automating a function where the user needs to click on the link highlighted in the below link.
HTML
I tried a contains on the xpath which is //*[#id="lc"], and an onclick option from Here neither of which work and error with the element doesn't exist error.
I know I am in the right iframe, because when using the xpath with no contains the script clicks the first xpath link with no issue.
To click() on the desired element as the element is a dynamic element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.EMROtherEpsEven>table tr>td.EMROtherEpsEven#lc[onclick*='Hemoglobin']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[#class='EMROtherEpsEven']/table//tr/td[#class='EMROtherEpsEven' and #id='lc'][contains(#onclick, 'Hemoglobin')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I have not use Selenium with Python, but I have with java, and in Java you have to use simple quote (') instead the double quote ("), or you can just a simpler locator id=lc without any quotes, if you're sure the ID is correct this should work.
i have done this in java hope you find the answer
yes in static or in dynamic tables the id will be the same but if you try using absolute xpath
this are two absolute path of the table element resides each other in first row
/html[1]/body[1]/section[1]/section[1]/div[1]/data[1]/div[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/div[1]
/html[1]/body[1]/section[1]/section[1]/div[1]/data[1]/div[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/div[2]/div[1]
This are the relative xpath of the same elements
//a[contains(text(),'AF17OT41603')]
//div[#id='1547533504703-0-uiGrid-000C-cell']//div[#class='ui-grid-cell-contents ng-binding ng-scope'][contains(text(),'AKSHAY PATIL')]
Table