2014-09-11 5 views
0

그래서 기본적으로 다음 웹 페이지에 셀렌을 사용합니다 : http://www.registrar.ucla.edu/schedule/catalog.aspx?sa=APPLING&funsel=3. 드라이버를 만들었고 특정 기준과 일치하는 대담한 텍스트를 찾고 있습니다. 다음은 굵은 텍스트를 검색하고 일치 찾아 내 코드의 일부입니다Selenium Python을 사용하여 다음 태그 찾기

# Finds all bold matches which contain the class title 
    bold_matches = driver.find_elements_by_class_name('bold') 
    class_title = '' 
    class_description = '' 
    for bold_match in bold_matches: 
     # If the class title matches, we set our class title 
     if bold_match.text[:bold_match.text.find('.')] == class_number: 
      class_title = bold_match.text 

당신은 사실 외에 너무 많은 코드에 대해 걱정할 필요가 없습니다은 우리가 일치하는 텍스트가 요소를 발견하면 , 텍스트를 클래스 제목으로 설정합니다.

내가 셀렌을 사용하는 데 도움이 필요한 것은 일치하는 텍스트의 다음 태그를 얻는 것입니다. 일치하는 태그 바로 뒤에 bold_match 태그가 필요합니다. 다음 태그는 class_description 텍스트를 설정할 텍스트를 포함합니다.

비슷한 질문을 살펴 보았지만 모두 xid와 일치하는 ID를 참조합니다. 이 웹 페이지의 문제점은 굵은 텍스트 태그와 그 뒤에있는 태그 뒤에 ID가 포함되어 있지 않다는 것입니다.

+0

'bold_match'요소의 자녀 또는 형제가 필요한 태그입니까? 나는 당신의 설명을 이해하지 못해 죄송합니다. –

+0

bold_match 요소의 형제가 필요합니다. 나는 실제로 해킹 방법을 발견하고 내 대답으로 게시 할 것입니다. 더 좋은 방법을 알고 있다면 알려주세요. – user2548635

답변

0

내가 원하는 것을 해킹하는 방법을 발견했습니다. 내가 발견 무엇

# Finds all bold matches which contain the class title 
bold_matches = driver.find_elements_by_class_name('bold') 
class_title = '' 
class_description = '' 
for bold_match in bold_matches: 
    # If the class title matches, we set our class title 
    if bold_match.text[:bold_match.text.find('.')] == class_number: 
     class_title = bold_match.text 

     # We find the class description from the class title 
     all_elements = driver.find_elements_by_xpath('//*') 
     matching_index = all_elements.index(bold_match) + 2 
     class_description = all_elements[matching_index].text 

난 그냥 현재 a ll_elements 목록에 일치하고 class_description을 얻을 수있는 다음 해당 인덱스를 찾을 요소의 인덱스를 찾을 수 있다는 것입니다 : 다음은 내 코드입니다.

+1

그건 certianly 작동하지만 당신은 '다음 형제'축 사용을 고려 했습니까? 'bold_match.find_element_by_xpath ("./ following-sibling :: tag_of_the_element_you_need") ' –

+1

@MarkRowlands 답변 해 주셔서 감사합니다. 어떤 webdriver 엘리먼트에 대해서도 find_element_by_xpath() 메소드를 사용할 수 있는지 몰랐다. – user2548635

관련 문제