1

how can i locate element td without name , id attributes?

<tr class="odd" onmouseover="this.className='highlight'" onmouseout="this.className='odd'">
        <td style="width:4%;">J199</td> 
        <td style="width:5%;">056962840</td>    
        <td style="width:3%;">S</td>    
        <td style="width:5%;">11</td>   
        <td style="width:7%;">0606353</td>  
        <td style="width:7%;">4846962</td>  
        <td style="width:3%;">1</td>    
        <td style="width:20%;">S1-4100181163-MANUAL</td>    
        <td style="width:5%;">2019-07-03</td>   
        <td style="width:5%;">&nbsp;</td>
        <td style="width:5%;cursor: pointer;"><span title="CSARL-SB/SINGAPORE PARTS DC">Y850</span></td>        
        <td style="width:5%;">2019-09-04</td>       
        <td>&nbsp;</td>
    </tr>

i only want to locate <td style="width:5%;">2019-09-04</td> and call it in python webdriver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
rob
  • 17
  • 6
  • Why do you say that you cannot access this element with XPath? Is there any particaluar reason that XPath cannot be used in your setup? – Kai Weber Aug 16 '19 at 07:55
  • oh sorry im new to selenium, is there a way to access this element with xpath? can u teach me – rob Aug 16 '19 at 08:13
  • Yes, it should be possible with XPath, but might require a little bit more context, possibly even the entire HTML document. If the element you want to select is always the second column from the right in all odd columns, the XPath would look like this: `//tr[@class = 'odd']/td[position() = last() -1]` Probably in your case you need to find a specific odd column within a table? Then the predicate for your `tr` would have to be adjusted. – Kai Weber Aug 16 '19 at 08:30
  • yes it worked. But there are 2 results. the `tr` falls under this table `id =boresulttable_table`. Is there a way to add this inside xpath and make sure there is only 1 result? Thanks! – rob Aug 16 '19 at 08:59
  • There are certainly ways to select the proper table and make sure there is only 1 result. But again, to make sure the XPath expression hits the right table cell requires some more context and knowledge about your application. Adding the table id gives `//table[@id = 'boresulttable_table']//tr[@class = 'odd']/td[position() = last() -1]` If this table has more than two odd rows, it will still give you more than one results, though. You can limit your XPath to only return the first result by appending `[1]` or to the second result with `[2]` to the end of the XPath expression. – Kai Weber Aug 16 '19 at 09:11
  • @Fenio , you tagged this question with Python, but I don't think this question is Python-related. Asking about locators in Selenium is quite agnostic to any particular language bindings. – Kai Weber Aug 16 '19 at 09:15
  • @KaiWeber Not necessarily. If you guy will have to provide the solution using `find_elements` and looping over them - it will also be `python` related. It's just an assumption – Fenio Aug 16 '19 at 09:17
  • 1
    @Fenio I see. My fight against the exclusion of XPath as a possible solution to rob's problem has blinded me a little and prevented me to think in the direction of more Python-specific solutions. – Kai Weber Aug 16 '19 at 09:22

2 Answers2

0

To locate the <td> with text as 2019-09-04 you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using xpath and element position:

    element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "//tr[@class='odd' and contains(@onmouseover, 'highlight')]//following-sibling::td[12]")))
    
  • Using xpath and title CSARL-SB/SINGAPORE PARTS DC:

    element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(@title, 'CSARL-SB/SINGAPORE PARTS DC')]//following::td[1]")))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

If you want to match <td> tag having the text of 2019-09-04 it would be something like:

//td[text()='2019-09-04']

If you want to match the <td> tag which is before the last <td> tag go for position() and last() functions combination:

//td[position()=last()-1]

More information: XPath Operators & Functions

Dmitri T
  • 159,985
  • 5
  • 83
  • 133